Docker easy to get started
Docker is an open source application container engine that can package applications into a portable image. Makes applications easier to deploy on any Linux or Windows operating system machines. At the same time, environmental isolation is also provided, which greatly avoids various problems caused by inconsistencies in different environments. The lightweight portability of Docker has also greatly promoted the development of CI/CD.
Docker architecture diagram
As can be seen from the figure, the components of Docker include:
docker client:dockercommand line tooldocker host: Host, that is, the running environment server ofdocker daemondocker daemon:dockerdaemon,docker clientinteracts withdocker daemonvia the command linecontainer: The smallest operating system environment that can containerize various services and applicationsimage: Mirror, can be understood as a template configuration of a container, and multiple containers can be started through one imageregistry: Mirror repository, which stores various images, can pull or push mirrors from the mirror repository.
Install
The following only explains the installation process on the CentOS server
CentOS Installation
- Installation dependencies
yum install -y yum-utils device-mapper-persistent-data lvm2- Add docker's yum image source. If it is in the country, add Alibaba Cloud's image source.
# Install docker official image source
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# If in the country, install the Alibaba Cloud image
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo- Install docker
# Install docker
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin- Start the service
systemctl enable docker
systemctl start dockerWhen the docker is installed successfully, you can use the following command to view the docker information
# View version information
docker --version
# View detailed version information
docker version
# View detailed configuration information
docker infoDaemon configuration
dockerd is the daemon of docker. dockerd can be configured through a configuration file. The configuration file location under linux is in /etc/docker/daemon.json. For more details, please refer to [official documentation] (https://docs.docker.com/engine/reference/commandline/dockerd/).
The log engine is json-file, which structures the logs and combines a suitable log system to facilitate log location. The storage engine is overrlay2.
mkdir /etc/docker
# Set docker daemon
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
# Restart docker
systemctl daemon-reload
systemctl restart dockerMirror
A mirror is a configuration file used to create a container. A container can be considered as a smallest operating system.
The image and container of docker are both used for hierarchical storage using unionFS. The image is shared as a read-only layer. The container attaches a writable layer to the image, minimizing waste of space.

Union file systems
UnionFS is a layered, lightweight and high-performance file system that supports layer-by-layer overlay of modifications to the file system as a commit. The mirrors and containers of docker are hierarchical storage, and the available storage engines include aufs, overlay, etc.
For details about hierarchical storage, you can view the official document docker: About storage drivers
Mirror warehouse and pull
In most cases, we do not need to build the image ourselves, and can directly pull it from the official image repository.
Use the command docker pull to perform mirror pull. View mirror information via docker inspect:
# Add to pull a node:alpine image
docker pull node:alpine
# View mirror information
docker inspect node:alpineBuild mirroring and publishing
Use the command docker build to build the image. docker build will use the dockerfile of the current directory to build the image.
Use -t to specify the tag
# -t node-base:10: Mirror and version number
# .: Refers to the current path
docker build -t node-base:10.When the image is built successfully, use docker push to push the image to the repository.
Dockerfile
When deploying your own application using docker, you often need to build your own image.
docker builds the image using Dockerfile as a configuration file:
FROM node:alpine
ADD package.json package-lock.json /code/
WORKDIR /code
RUN npm install --production
ADD . /code
CMD npm startFROM
Based on an old mirror, the format is as follows
FROM <image> [AS <name>]
# It will be used during multi-stage construction
FROM <image>[:<tag>] [AS <name>]ADD
Add directory, or url address files to the mirrored file system
ADD [--chown=<user>:<group>] <src>... <dest>RUN
Execute the command, because of the ufs file system, it will add a new layer to the top layer of the current image
RUN <command>CMD
Specify how the container starts
Only one CMD is allowed in a Dockerfile
# exec form, this is the preferred form
CMD ["executable","param1","param2"]
# as default parameters to ENTRYPOINT
CMD ["param1","param2"]
# shell form
CMD command param1 param2Container
The relationship between mirror and container is similar to the relationship between code and process.
docker runcreates containerdocker stopStop the containerdocker rmdelete container
Create container
Create the easiest container based on the nginx image: start the easiest http service
Use docker run to start the container, and docker ps to view the container startup status
docker run -d --name nginx -p 8888:80 nginx:alpine
docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
404e88f0d90c nginx:alpine "nginx -g 'daemon of..." 4 minutes ago Up 4 minutes 0.0.0.0:8888->80/tcp nginx
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESin:
-d: Start a daemon process--name: Specify a name for the container-p host-port:container-port: Mapping of host and container ports to facilitate container services to the outside worldnginx:alpine: Create container based on this image
At this time, use curl on the host to test whether the service provided by the container is normal
curl localhost:8888Enter the container environment and use the docker exec -it container-name command
docker exec -it nginx sh
/ #
/ #
/ #Container Management
docker pslists all containers
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
404e88f0d90c nginx:alpine "nginx -g 'daemon of..." 4 minutes ago Up 4 minutes 0.0.0.0:8888->80/tcp nginx
498e7d74fb4f nginx:alpine "nginx -g 'daemon of..." 7 minutes ago Up 7 minutes 80/tcp lucid_mirzakhani
2ce10556dc8f redis:4.0.6-alpine "docker-entrypoint.s..." 2 months ago Up 2 months 0.0.0.0:6379->6379/tcp apolloserverstarter_redis_1docker portView container port mapping
docker port nginx
80/tcp -> 0.0.0.0:8888docker statsView container resource usage
docker stats nginx
CONTAINER ID NAME CPU %MEM USAGE / LIMIT MEM %NET I/O BLOCK I/O PIDS
404e88f0d90c nginx 0.00% 1.395MiB / 1.796GiB 0.08% 632B / 1.27kB 0B / 0B 2