4.1 Introduction to Docker
4.1.1 Project deployment issues
There are many components in large-scale projects, and the operating environment is also relatively complex. Some problems will be encountered during deployment:
1. The dependency relationship is complex, prone to dependency conflicts and compatibility issues
2. The development, testing, and production environments are different, and the cost of switching environments is high
4.1.2 How Docker solves the problem
- Package the application's Libs (function library), Deps (dependency), configuration and application together
- Run each application in an isolated container to avoid mutual interference
4.1.3 Images and containers
Mirror (Image) : Docker packages the application and its required dependencies, function libraries, environment, configuration and other files together, called a mirror
Container (Container) : The process formed after the application in the mirror is running is a container, but docker will isolate the container, which is invisible to the outside world
4.2 Install Docker on CentOS
4.2.1 If you have installed an old version of Docker before, you can use the following command to uninstall it:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
docker-ce
4.2.2 Install the yum tool
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2 --skip-broken
4.2.3 Update the local mirror source
# 设置docker镜像源
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
yum makecache fast
4.2.4 Enter the command to install docker
yum install -y docker-ce
4.2.5 Start docker
1. Turn off the firewall first, because the image configured by docker needs to use various ports, it is recommended to turn off the firewall directly
# 关闭
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
2. Start docker by command:
systemctl start docker # 启动docker服务
systemctl stop docker # 停止docker服务
systemctl restart docker # 重启docker服务
3. View the docker version through the docker - v command
4.3 Basic operation of Docker
4.3.1 crud command of Docker image
1. Common commands
docker build #构建镜像
docker pull [repostitory]:[tag] #mysql:5.7,从镜像服务器拉取镜像
docker push #将镜像推送到镜像服务器
docker save #保存镜像为一个压缩包
docker load #加载压缩包为镜像
docker images #查看镜像
docker rmi #删除镜像
2. There are many docker commands, you need to learn to view help documents
docker --help
It is also possible to further describe the command to look at
4.4 docker operation example, take nginx as an example
4.4.1 Enter https://hub.docker.com/ to search for nginx images
After clicking, there is a corresponding pull code, just copy it, and the latest version is defaulted if the version is not specified
4.4.2 Practical operation (mirror command)
1. Use docker pull to pull the nginx image (recommended to add: latest)
2. Use docker images to view the image, and the pull is successful
3. Use the docker save command to turn the image into a .tar archive
docker save -o nginx.tar nginx:latest
**4.** Delete the nginx image, and then pull the image by loading the compressed package ( if you delete an error, you can directly add the -f parameter to force the deletion )
**5.** Load the image in the compressed package through the load command
docker load -i nginx.tar
4.4.3 Container commands
4.4.4 Create and run an Nginx container
**1.**The same as when pulling the image, search for the nginx image on DockerHub, find the location, copy and paste the command
2. Command Interpretation
docker run --name some-nginx -d -p 8080:80 some-content-nginx
#docker run: 创建并运行一个容器
#--name:给容器起一个名字,比如叫做mn(mynignx)
#-p: 将宿主机端口与容器端口映射,冒号左侧是宿主机端口,右侧是容器端口
#-d:后台运行容器
#some-content-nginx:镜像名称,例如nginx
3. Practical operation (after successful execution, a container id will be returned)
**4.** Use docker ps to check whether the container is created successfully
5. Use a browser to access the nginx service in the virtual machine
**6.** View the logs generated by nginx ( the -f parameter needs to be added to the continuous output logs )