Installation and use of docker

Installation and use of docker

1. Virtualization

Virtualization is a resource management technology, which is abstracted from computer hardware resources, breaking the indivisible barrier of hardware entities, and users can configure these hardware resources according to their needs. The new virtual part of these resources is not restricted by the way existing resources are erected, geographical or physical configuration. Generally referred to as virtualized resources include computing power and data storage.

Application: such as cloud server, virtual machine, etc.

2. docker introduction

Docker is an open source application container engine, based on the Go language and open source in compliance with the Apache2.0 protocol.

Docker allows developers to package their applications and dependent packages into a lightweight, portable container, and then publish to any popular Linux machine, it can also be virtualized.

Containers use the sandbox mechanism completely, and there will be no interfaces between each other (apps similar to iPhone), and more importantly, the container performance overhead is extremely low.

About the version

The 17 in docker's 17.0x refers to 2017. Looking at it now, the beginning of 17 is the old version, and the newer version is 19.xx

3. container and virtual machine comparison

We need to install the operating system in the virtual machine, and then install the software we need on the operating system.

Install docker on host os, and then we can go to the docker warehouse to pull the software we need. These software is equivalent to the Linux operating system, but it is much smaller than the directly installed Linux operating system. Containers are isolated from each other. A docker engine can start multiple containers at the same time. These containers do not interfere with each other. We can also start the operating system on docker.

4. the advantages of docker

Docker can quickly deliver and deploy projects, more efficient virtualization, easier red migration and expansion, and simpler management.

5. docker's architecture

Docker engine

The docker engine is an application of c/s structure, and the main components are shown in the following figure:

  • Server is a resident process
  • REST API implements the interaction protocol between client and server
  • CLI realizes the management of containers and images, and provides users with a unified operation interface
  • image is a mirror
  • container is the container


Docker containers are created through Docker images.

The relationship between containers and mirrors is similar to objects and classes in object-oriented programming.

Docker ------> object-oriented

Container------> Object

Mirror------> Class

Docker architecture

Docker uses the C/S architecture, and the Client communicates with the Server process through the interface to implement the construction, operation and release of the container. The client and server can run on the same cluster, or they can communicate remotely through cross-hosts.

client: client

docker_host: host host

registry: Warehouse: private server and central warehouse (Docker Hub)

core concept


The ocker image is a template for creating Docker containers


A container is an application or a group of applications that run independently


The Docker client uses the Docker API ( ) to communicate with the Docker daemon through the command line or other tools .


A physical or virtual machine is used to execute the Docker daemon and container.

Warehouse (Registry)

The Docker warehouse is used to store images, which can be understood as a code warehouse in code control.

Docker Hub ( ) provides a huge collection of images for use.

6. docker installation

Docker supports windows, Linux, and macOS operating systems. The official recommendation is to use the Linux Ubuntu operating system, because Docker is based on Ubuntu, and Ubuntu is the first to be updated or patched when it comes to problems with Docker.

Because of the centos I use, I will only introduce the installation of docker on centos for the time being. It is recommended to install the version above CentOS7.x. In the version of CentOS6.x, many other environments need to be installed before installation, and many patches of docker are not Support updates.

Step 1: Update the yum package to the latest

sudo yum update

Step 2: Install the required software packages

yum-util provides the yum-config-manager function, the other two are dependent on the devicemapper driver

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Step 3: Set yum source to Alibaba Cloud

sudo yum-config-manager --add-repo

Step 4: Install docker

sudo yum install docker-ce

Step 5: Check the docker version after installation

docker -v

Step 6: Set up the mirror image of ustc

Ustc is an old linux mirroring service provider, and it was still in use in the distant ubuntu 5.04 version. ustc's docker image accelerator is very fast. One of the advantages of ustc docker mirror is that it does not require registration and is a true public service.

Edit the file:


Enter the following in the file:

"registry-mirrors": [""]
#Configure the source (if you can't start it, you don't strictly follow the josn format, just check it)

It is best to set the mirror source of ustc to be a domestic source, otherwise the speed of pulling mirrors from the warehouse will be very slow. I personally think that the domestic Qiniu Cloud and Alibaba Cloud mirror sources will be faster. Alibaba source needs to log in to Alibaba Cloud to apply, and Alibaba will open a source separately for you, and the above is the application.

The configuration needs to restart the docker service

systemctl restart docker 

Step 7: Start and stop Docker

The systemctl command is a system service manager command

Start docker:

systemctl start docker

Stop docker:

systemctl stop docker

Restart docker:

systemctl restart docker

View docker status:

systemctl status docker


systemctl enable docker

View docker summary information

docker info

View the docker help documentation

docker --help

7. docker mirror operation

View mirror

docker images

REPOSITORY: mirror name

TAG: Mirror tag


CREATED: The creation date of the image (not the date when the image was obtained)

SIZE: image size

These images are stored in the/var/lib/docker directory of the Docker host

docker images -q

View the IMAGE ID of all mirrors

Search mirror

If you need to find the required mirror from the network, you can search through the following command

docker search image name

NAME: Warehouse name

DESCRIPTION: mirror description

STARS: User reviews, reflecting the popularity of a mirror

OFFICIAL: Is it official

AUTOMATED: Automatically build, indicating that the image was created by the Docker Hub automatic build process

Pull mirror

To pull a mirror is to download the mirror from the central warehouse to the local

docker pull image name

For example, I want to download the centos7 mirror

docker pull centos:7

Delete mirror

Delete mirror by mirror ID

docker rmi image ID

Delete all mirrors (use with caution)

docker rmi `docker images -q`

8. docker container operation

View container

View running containers

docker ps

View all containers

docker ps –a

View the last run container

docker ps –l

View stopped containers

docker ps -f status=exited

Create and start the container

Description of commonly used parameters for container creation:

Create container command: docker run

-i: means running the container

-t: indicates that the container will enter its command line after it is started. After adding these two parameters, the container creation can log in. That is, a pseudo terminal is allocated.

--name: Name the created container.

-v: Represents the directory mapping relationship (the former is the host directory, the latter is the directory mapped to the host), you can use multiple -v to do multiple directory or file mapping. Note: It is best to do directory mapping, make changes on the host, and then share to the container.

-d: Add the -d parameter after run, it will create a guardian container to run in the background (so that the container will not be automatically logged in after the container is created, if only the -i -t two parameters are added, it will automatically enter after creation container).

-p: Represents port mapping, the former is the host port, and the latter is the mapping port in the container. You can use multiple -p to do multiple port mapping

(1) Create a container interactively

docker run -it --name=container name image name: label/bin/bash

docker run -di --name=mycentos2 centos:7
# docker run -d -i --name=mycentos2 centos:7	
# Each container has its own id number: b1eeace155b54e467dfcbc42bb2fa5b580db5463f230a3eae1b27b74de4c597

docker run -d -i --name=mycentos3 centos:6.5 # If the latest centos image is not available locally, pull it first, then create and allow it
# Start a redis container

docker run -di --name=myredis -p 6379:6379 redis
# Stop the reids of this machine
# Port mapping, 6378 6379

# Use docker to allow a service (install a software), it becomes very simple
-Linux installation redis (source download, decompression, make & make insall)

At this time, we use the ps command to view and find that we can see the started container, and the status is started.

Exit the current container


(2) Create a container in a guardian way:

docker run -di --name=container name image name: label

Log in to the guardian container:

docker exec -it container name (or container ID)/bin/bash

Stop and start the container

Stop the container:

docker stop container name (or container ID)

Start the container:

docker start container name (or container ID)

File copy

If we need to copy files to the container, we can use the cp command

docker cp file or directory to be copied Container name: container directory

You can also copy files from the container

docker cp container name: the file or directory that needs to be copied in the container directory

Directory mount

When creating the container, we can map the directory of the host machine to the directory in the container, so that we can modify the file in a certain directory of the host machine to affect the container. Create a container and add the -v parameter to the host directory: container directory, for example:

docker run -di -v/usr/local/myhtml:/usr/local/myhtml --name=mycentos3 centos:7

If you are sharing a multi-level directory, you may be prompted with insufficient permissions.

This is because the security module selinux in CentOS7 has disabled the permissions, we need to add the parameter --privileged=true to solve the problem that the mounted directory does not have permissions

View container IP address

We can view various data of the container running through the following commands

docker inspect container name (container ID) 

You can also directly execute the following command to directly output the IP address

docker inspect --format='{{.NetworkSettings.IPAddress}}' container name (container ID)

Delete container

Delete the specified container:

docker rm container name (container ID)
Reference: docker installation and use-cloud + community-Tencent Cloud