Unveil docker (basic theory and installation details)
Preface
This article will focus on the following aspects to unveil docker:
- What is docker?
- Why do I need docker?
- The core concept of docker
What is docker?
Based on Linux, docker is an open source tool to run applications in it, which can be considered as a lightweight virtual machine. Docker's logo is a whale carrying a container (the old logo). The new logo directly simplifies the details and changes the color, as shown below.
As you can see from the picture, the creation of docker and the intention of developers. Whale - host, blockbox - isolated container (what is docker container later). The purpose of docker: Build, Ship and Run Any APP, Anywhere. Specifically, it is to achieve the goal of "once encapsulation, everywhere operation" at the application component level through the management of the application component's encapsulation, release, deployment, operation and other life cycle.
For example, if you have developed an application software, you can implement it based on docker when it can be used in any place, any time and any operating system. Of course, for docker, this component can be an application, a configuration, or even a complete operating system.
Why do I need docker?
In fact, docker is a great boon for developers. Imagine that the current or future development of applications or software based on cloud platform is separated from the underlying hardware, and users need to be able to access relevant resources at any time and any place, then docker can provide such a demand.
The comparison between docker container and traditional virtual machine is given directly below
Characteristic | Docker container | virtual machine |
---|---|---|
Starting speed | SEC level | Minute class |
Computer performance loss | Almost no loss | Loss of about half |
performance | Near native | Weak |
System support | Thousands of | Dozens of |
Isolation | Resource constraints | Complete isolation |
The core problem that docker solves is to use containers to implement VM like functions, so as to provide users with more computing resources with more economical hardware resources, and docker is easy to operate. It can also support flexible automatic creation and deployment through Dockerfile configuration file.
Next, let's look at the comparison between Docker and traditional virtual machine architecture:
According to the above figure, the virtual machine is isolated at the physical resource level. Compared with the virtual machine, Docker is isolated at the APP level, and the virtual machine operating system is omitted, thus saving part of the system resources.
The Docker daemons can directly communicate with the main operating system to allocate resources for each Docker container; it can also isolate the container from the main operating system (for security) and isolate each container from each other. The virtual machine starts in minutes, and the Docker container can start in milliseconds. Because there is no bloated slave operating system, Docker can save a lot of disk space and other system resources.
In short, the difference between virtual machine and docker is that virtual machine needs to virtualize hardware resources through Hypervisor, and docker directly uses hardware resources of host machine, so the isolation of virtual machine is more thorough, followed by that of docker.
The core concept of Docker
1. Image image
It can be understood that like the snapshot in our virtual machine, it is the basis for creating a container. It can be said that there is no image and no container.
2. Container - container
The running instance created based on the image can be started, stopped and deleted. Each container is isolated from each other.
3. Warehouse - repository
The place where images are saved can be understood as image hoarding. You can push your own creation to the warehouse, which is convenient to pull and use anytime and anywhere.
The warehouse registration server is the place to store the warehouse, which contains multiple warehouses. Each warehouse stores a kind of image, and uses different tag s to distinguish. At present, the largest public warehouse is Docker Hub, which stores a large number of images for users to download and use.
Here is the docker architecture:
Docker uses the client server architecture. The docker client has a dialogue with the docker daemons, which complete the heavy work of building, running and distributing the docker container. The docker client and the daemons can run on the same system, or you can connect the docker client to the remote docker daemons. Docker clients and daemons use rest APIs on UNIX sockets or network interfaces to communicate.
Architecture diagram:
Installation process of Docker
The installation and deployment process based on Linux/Centos7 is as follows:
Operating system version: CentOS 7.7
1. Environment deployment
[root@localhost ~]# hostnamectl set-hostname docker [root@localhost ~]# su [root@docker ~]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core) [root@docker ~]# systemctl stop firewalld && systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@docker ~]# setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config [root@docker ~]# echo "net.ipv4.ip_forward=1" > /etc/sysctl.conf #Turn on route forwarding [root@docker ~]# sysctl -p #Load route forwarding function net.ipv4.ip_forward = 1 [root@docker ~]# systemctl restart network
2. Install dependency package
[root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 ... //Update completed: lvm2.x86_64 7:2.02.185-2.el7_7.2 //Upgraded as a dependency: device-mapper.x86_64 7:1.02.158-2.el7_7.2 device-mapper-event.x86_64 7:1.02.158-2.el7_7.2 device-mapper-event-libs.x86_64 7:1.02.158-2.el7_7.2 device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2 lvm2-libs.x86_64 7:2.02.185-2.el7_7.2 //Complete!
Explain:
The yum utils package is installed to provide a Yum config manager unit. At the same time, two packages, device mapper persistent data and lvm2, are installed for storage device mapping.
3. Set up a stable repository
[root@docker ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo //Loaded plug-ins: faststmirror, langpacks adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo [root@docker ~]# ls /etc/yum.repos.d/ #Check whether a docker source has been added CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo docker-ce.repo
4. Install docker engine
[root@docker ~]# yum install -y docker-ce ... //Installed: docker-ce.x86_64 3:19.03.8-3.el7 //Installed as a dependency: container-selinux.noarch 2:2.107-3.el7 containerd.io.x86_64 0:1.2.13-3.1.el7 docker-ce-cli.x86_64 1:19.03.8-3.el7 //Complete!
5. Create docker directory and configure image acceleration service
[root@docker ~]# mkdir /etc/docker -p [root@docker ~]# tee /etc/docker/daemon.json <<-'EOF' > { > "registry-mirrors": ["https://5m9y9qbl.mirror.aliyuncs.com"] > } > EOF { "registry-mirrors": ["https://5m9y9qbl.mirror.aliyuncs.com"] }
Note: tee command -- write from standard input to FILE and standard output, that is, copy the data of standard input to each FILE, and send it to standard output at the same time.
6. Open service, view version
[root@docker ~]# systemctl daemon-reload [root@docker ~]# systemctl restart docker.service [root@docker ~]# docker -v Docker version 19.03.8, build afacb8b [root@docker ~]# docker version Client: Docker Engine - Community Version: 19.03.8 API version: 1.40 Go version: go1.12.17 Git commit: afacb8b Built: Wed Mar 11 01:27:04 2020 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.8 API version: 1.40 (minimum version 1.12) Go version: go1.12.17 Git commit: afacb8b Built: Wed Mar 11 01:25:42 2020 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.13 GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc: Version: 1.0.0-rc10 GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker-init: Version: 0.18.0 GitCommit: fec3683
summary
This paper mainly introduces the function of docker, compares its characteristics with traditional virtual machine, and compares its architecture. Then it introduces the three core components of docker, and finally gives the installation process of docker in Centos7 operating system based on Linux.