This is the second article of Ceph practice column. First of all, this article will briefly introduce the basic concepts of Docker. Next, it will introduce deploying Docker on CentOS 7.

Basic concepts of Docker

What is a container

A container is a tool that can hold other items, such as cabinets and boxes in daily life. A container in software is a process that runs in isolation. If the process stops, the container is destroyed. An isolated environment with its own system files, IP addresses, host names, etc. Containers are completely sandbox environments with no interfaces to each other. With containers out of the way, what’s the difference between a container and a virtual machine?

Container virtualization and KVM virtualization are different

  1. KVM virtualization: Hardware support is required (first install hypervisors (vmware, KVM, and HyperV), and analog hardware is required to run different operating systems. The startup time is minute (startup process).
  2. No need for hardware support, no need to simulate hardware (directly install Docker Engine on the system, then directly install Docker, no Hypervisor), shared host kernel, startup time in seconds (no startup process)

Advantages of Docker over VM

  1. With fewer resources, one machine can run thousands of Docker containers
  2. Lightweight, fast start and stop, second level implementation
  3. Faster delivery and deployment, once created and configured, can run anywhere.
  4. Kernel-level virtualization, which does not require additional Hypevisor support, provides higher performance and efficiency
  5. Easy to migrate, platform dependence is not strong

disadvantages

  1. Less isolated than VMS
  2. Limited network configuration, no way to get through the network

The introduction of Docker

Docker is a software packaging technology for creating, managing, and orchestrating containers:

  1. Build: Make a Docker image
  2. Shipping: Docker Pull
  3. Run: Starts a container

For each container, it has its own system file rootfs. KVM solves the dependency between hardware and operating system KVM standalone virtual disks and THE XML configuration file Docker solves the dependency between software and operating system environment, enabling applications to run the same way almost anywhere (one build, run everywhere). Developers can create and test containers on their own laptops and run them without modification on virtual machines in production systems, physical servers, or public cloud hosts. Docker uses kernel virtualization technology (Namespaces and Cgroups CPU, memory, disk IO, etc.) to provide resource isolation and security for containers. Therefore, Docker containers do not require additional operating system overhead similar to virtual machines (VMS) to improve resource utilization. Namespace Resource isolation Resource limit of the Cgroups process KVM virtual disk file, resource isolation KVM resource limit, –cpus –memory

The composition of the Docker

A complete Docker has six components.

  1. Docker Host: A physical machine or virtual machine used to run Docker service processes and containers.
  2. Docker Client: The Client invokes the Docker API using Docker commands or other tools.
  3. Docker Daemon: A Docker Daemon that runs Docker containers.
  4. Docker Image: An Image can be understood as a template for creating instances.
  5. Docker Container: A Container is a service or group of services that are generated from an image.
  6. A version control system such as Git or SVN. Official repository: hub.docker.com/

Here’s how the Docker works:

If Docker is compared with object-oriented, then the mirror is a static definition, corresponding to object-oriented classes, and the container is the entity of the mirror runtime. Corresponds to objects in object oriented. Containers can be created, started, stopped, deleted, paused, and so on.

The installation of a Docker

1. Uninstall the original version:

sudo yum remove docker docker-common docker-selinux docker-engine
Copy the code

2. Install dependency packages:

yum -y install yum-utils device-mapper-persistent-data lvm2
Copy the code

Configure the YUM source:

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code

4. Install Docker. To avoid version compatibility problems, specify the version here;

Yum -y install docker - ce - 19.03.9Copy the code

5. Start the Docker

systemctl start docker
Copy the code

6. Set the Docker to start automatically

systemctl enable docker
Copy the code

7. Configure the mirror accelerator

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://6a6e85x9.mirror.aliyuncs.com"]
}
EOF
Copy the code
  1. Restart the Docker Daemon
sudo systemctl daemon-reload
Copy the code

9. Restart Docker

sudo systemctl restart docker
Copy the code

Docker application scenarios

Scenario 1: Save project environment deployment time

Every time you deploy a project to a test, production, or other environment, you have to deploy a lot of dependent software and tools, and there is a high probability of problems during deployment, which can take a long time. The main concept of Docker is that the environment can be packaged and deployed to run on any Docker Engine. In the early stage, we just need to package each project environment into the image and push it to the image repository. When it is necessary to deploy the project, we can directly pull the image to start the container and the project can be accessed! One build, running everywhere.

Scenario 2: Environment consistency

Docker packages the project environment into an image, which can be used on any Docker Engine. At this time, Docker is the cornerstone of our projects, Docker portability, and consistency of running state.

Scenario 3: Package and version management;

Docker has learned the mode of Git and imitated Github to derive DockerHub, that is, mirror warehouse. Meanwhile, Docker can also have its own private warehouse. The use of warehouse is a major advantage of Docker in the process of CD.

Scenario 4: Microservices

Microservices are as fine-grained as possible to split the business program architecture, composed of multiple independent services business system, Docker container design principle: a container a service, containers are isolated from each other, does not hinder to think, if the container as the deployment unit of these independent services, is not appropriate.

Docker common commands

Kill all processes under the Docker and delete the container

docker kill $(docker ps -q); docker rm -f $(docker ps -a -q)
Copy the code

Modify the docker configuration file

vim /etc/systemd/system/multi-user.target.wants/docker.service
Copy the code

Test whether docker is started successfully

docker run hello-world
Copy the code

Check whether the container is running

docker ps -a
Copy the code

Found that the container is not in up state, want to find the reason:

docker logs -f docker_name
Copy the code

For example, docker logs -f mon

The container’s on. I want to get in there

docker exec docker_name
Copy the code

I don’t want to play anymore, I want to exit the container (without killing the container)

The exit or CTRL + DCopy the code

View the container details

docker inspect docker_name
Copy the code

Look at how many resources the container occupies

docker stats docker_name
Copy the code

Want to stop, restart, and kill the container

docker stop|restart|rm -f docker_name
Copy the code

Mirror related knowledge points

1. Check the available mirrors

docker images
Copy the code

2. How do I view details about a mirror

docker inspect [REPOSITORY:TAG]  IMAGE ID 
Copy the code

3. Tags mirror

Docker tag redis 172.22.67.38:5000 / redisCopy the code

4. Upload the image to my private warehouse

Docker push 172.22.67.38:5000/redis pull/rm pull /redis pull/rm pull /redis pull/rm You can -f.Copy the code

5. Build a private mirror warehouse

docker run -d -p 5000:5000 --restart=always --name registry -v /data/registry/data:/var/lib/registry registry:2
Copy the code

conclusion

This article first introduces the basic concept of container, container is a process in isolation environment, any operation in the container will not affect the host machine. Then it compares container virtual machines with KVM virtualization. The most important difference is that container virtualization does not require hardware support and does not require a Hypervisor to be installed. KVM virtualization does. Then it introduces the installation and common commands of Docker.

reference

Introduction and features of Docker