Docker Learning (2) Detailed explanation of images and container data volumes

7. Graphical management tools

The content of this article is summarized from the content of Docker Quick Start (1) ;

Portaniner is a graphical management tool for Docker. It provides a background panel for us to operate, similar to tools such as database visualization panel SQLyog.

1. Download and install

# -p 8088:9000  			 # 内部9000端口映射外部端口为8088
# -v /var/run/docker.sock:/var/run/docker.sock 	(这是挂载技术,后面再学习)
# --privileged=true 		# 授权可以访问
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer


[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
94cfa856b2b1: Already exists 
49d59ee0881a: Already exists 
a2300fd28637: Already exists 
Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
Status: Downloaded newer image for portainer/portainer:latest
3cad49e5d24c55ba9aebbcde2d593f948e7aba91751ae27148dac709314fab14
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND             CREATED         STATUS         PORTS                    NAMES
3cad49e5d24c   portainer/portainer   "/portainer"        8 seconds ago   Up 8 seconds   0.0.0.0:8088->9000/tcp   jovial_perlman
3e7fc95c278a   tomcat                "catalina.sh run"   9 hours ago     Up 9 hours     0.0.0.0:3355->8080/tcp   tomcat01
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# 

2. Access through the external network, http://server ip address: 8088

Alibaba Cloud server sets a security group. When logging in for the first time, you need to set the password of the admin user.
Insert image description here

After setting the password and creating the user, enter the page below and select local connection.

Insert image description here

This is the overall visual interface preview:

Insert image description here

Insert image description here

8. Detailed explanation of docker image

8.1. What is a mirror?

An image is a lightweight, executable independent software package used to package software running environments and software developed based on the running environment. It contains everything needed to run a certain software, including code, runtime dependencies, and libraries. Environment variables and configuration files.

8.2. Loading principle of docker image

8.2.1. Union File System (UnionFS)

Union File System (UnionFS):

  • Developed by the State University of New York in 2004, it can jointly mount the contents of multiple directories into the same directory, while the physical locations of the directories are separate. UnionFS can merge read-only and read-write file systems, and has a copy-on-write function, allowing modifications to the read-only file system to be saved to the writable file system.

  • It is a layered, lightweight, high-performance file system. It supports modifications to the file system to be superimposed layer by layer as a single submission. At the same time, different directories can be mounted to the same virtual file system.

  • It is the basis of Docker images. Images can be inherited through layering. Based on the base image (without a parent image), various specific application images can be produced.

Features: Load multiple file systems at the same time, but from the outside, only one file system can be seen. Joint loading will superimpose each layer of file systems, so that the final file system will contain all underlying files and directories.

8.2.2. Docker image loading

Insert image description here

bootfs (boot file system) mainly includes bootloader and Kernel. The bootloader is mainly for booting and kernel. The bootfs file system will be loaded when Linux first starts. The bottom layer of the Docker image is bootfs. This layer is the same as our typical Linux/Unix system, including the boot loader and kernel. When the boot loading is completed, the entire kernel is in the memory. At this time, the right to use the memory has been transferred from bootfs to the kernel. At this time, the system will also uninstall bootfs.

rootfs (root file system), on top of bootfs. Contains standard directories and files such as /dev, /proc, /bin, /etc in typical Linux systems. Rootfs refers to various operating system distributions, such as Ubuntu, Centos, etc.

Insert image description here

Usually the CentOS we install into the virtual machine is several GB. Why is Docker only 230 MB?

For a streamlined OS, rootfs can be very small and only needs to contain the most basic commands, tools and libraries. Because the bottom layer directly uses the host's kernel, you only need to provide rootfs. It can be seen that for different Linux distributions, bootfs is basically the same, but rootfs will be different, so different distributions can share bootfs.

8.3. Understanding of layering

The docker image is actually composed of layers of file systems. As mentioned above, when downloading the image through docker pull, you can see that the docker image seems to be downloaded layer by layer. When downloading different versions of an image locally, there are common file parts between the different versions of the image. If this part already exists locally, it will not be downloaded repeatedly and will be shared directly.

Reasons for stratification:

  1. The biggest advantage of layering is == shared resources ==
  2. If multiple images are built from the same base image, the host only needs to save a copy of the base image on the disk;
  3. At the same time, only one base image is loaded into the memory to serve all containers, and each layer of the image can be shared.

Download a mysql 5.7 version image in the virtual machine and observe the downloaded log output, as shown in the figure below. The downloading process is downloaded layer by layer. If the previous layers already exist locally, there is no need to download them again. .

Insert image description here

Insert image description here

View the image layer: docker image inspect image name

Insert image description here

Insert image description here

Layered understanding:

All Docker images start from a basic image layer. When the content is modified or new content is added, a new image layer will be created on top of the current image layer. For example, if you create a new image based on Ubuntu Linux 16.04, this is the first layer of the new image. When you add python-related content to this image, a second layer of images will be created on top of the first layer. As you continue to add security patches (Security Patch), a third layer image will be created.

Insert image description here

For another example, the first-level image contains files 1, 2, and 3. Later, a second-level image is created, which contains files 4, 5, and 6. Later, the image content needs to be modified to upgrade file 5 to file 7. , new updates and modifications require the creation of a third-layer image. In this case, the files in the upper image overwrite the files in the lower image. This causes the updated version of the file to be added to the image as a new image layer. So from the outside, the entire image is divided into three layers and contains a total of 6 files, of which file 7 is an updated version of file 5.

Insert image description here

Dokcer images are read-only. When the container starts, a new writable layer will be loaded on top of the image. This new layer is commonly referred to as the container layer, and the one below the container is the image layer.

8.4. Commit to submit the image

There are two ways to build an image:

  • One is to create a mirror through commit
  • The other is to build the image through dockerfile (the dockerfile construction method will be learned later)

After the container is submitted through commit, the container can be submitted as a new image version.

# docker commit -m=“提交的描述信息” -a=“作者信息” 容器id 目标镜像名:[TAG]

As mentioned above about the deployment of tomcat, the tomcat image downloaded by default is the smallest image to ensure the smallest operating environment. The webapps directory in the image is empty. You need to enter the container and copy the contents of the webapps.dist directory to webapps before accessing it from the external network through port mapping. To this end, create a new tomcat image as follows: Submit a fully operational tomcat container as a new image. This allows the image to be used directly after downloading it, without the need to copy it.

Steps to make tomcat image:

# 1. 下载默认的tomcat镜像
docker pull tomcat
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker pull tomcat  # 下载tomcat镜像
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Already exists 
9b829c73b52b: Already exists 
cb5b7ae36172: Already exists 
6494e4811622: Already exists 
668f6fcc5fa5: Already exists 
dc120c3e0290: Already exists 
8f7c0eebb7b1: Already exists 
77b694f83996: Already exists 
0f611256ec3a: Already exists 
4f25def12f23: Already exists 
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker images		#查看是否下载成功
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
elasticsearch         8.2.0     f75ee9faf718   3 weeks ago     1.21GB
nginx                 latest    605c77e624dd   4 months ago    141MB
tomcat                latest    fb5657adc892   4 months ago    680MB
redis                 latest    7614ae9453d1   4 months ago    113MB
mysql                 latest    3218b38490ce   4 months ago    516MB
hello-world           latest    feb5d9fea6a5   7 months ago    13.3kB
centos                latest    5d0da3dc9764   7 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   13 months ago   79.1MB
# 2. 启动这个tomcat镜像,
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker run -it -p 8023:8080 tomcat
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/openjdk-11
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
# 3. 查看正在运行的容器
docker ps
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND             CREATED         STATUS         PORTS                    NAMES
e315dfa9c866   tomcat                "catalina.sh run"   2 minutes ago   Up 2 minutes   0.0.0.0:8023->8080/tcp   strange_brown
3cad49e5d24c   portainer/portainer   "/portainer"        14 hours ago    Up 14 hours    0.0.0.0:8088->9000/tcp   jovial_perlman
# 4。 进入tomcat容器内部,查看webapps目录,没有内容,将webapps.dist目录中的内容拷贝到webapps目录中
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker exec -it e315dfa9c866 /bin/bash
root@e315dfa9c866:/usr/local/tomcat# ls
BUILDING.txt  CONTRIBUTING.md  LICENSE	NOTICE	README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
root@e315dfa9c866:/usr/local/tomcat# cd webapps
root@e315dfa9c866:/usr/local/tomcat/webapps# ls
root@e315dfa9c866:/usr/local/tomcat/webapps# cd ..
root@e315dfa9c866:/usr/local/tomcat# cd webapps.dist
root@e315dfa9c866:/usr/local/tomcat/webapps.dist# ls
ROOT  docs  examples  host-manager  manager
root@e315dfa9c866:/usr/local/tomcat/webapps.dist# cd ..
root@e315dfa9c866:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@e315dfa9c866:/usr/local/tomcat# cd webapps
root@e315dfa9c866:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager
root@e315dfa9c866:/usr/local/tomcat/webapps# 

# 4. 将操作之后的容器通过commit提交成一个新的tomcat镜像,以后便可以直接下载这个新的镜像使用。
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker commit -m="new tomcat that added app to webapps" -a="Kevin-Ding" e315dfa9c866 mytomcat:1.0
sha256:fae635bb688d1260244034fe2ed158f28de3bd0fa5cc7269cb77edcc0d5b6931
# 5. docker images 便可查看到自己提交的镜像 mytomcat
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
mytomcat              1.0       fae635bb688d   28 seconds ago   684MB
elasticsearch         8.2.0     f75ee9faf718   3 weeks ago      1.21GB
nginx                 latest    605c77e624dd   4 months ago     141MB
tomcat                latest    fb5657adc892   4 months ago     680MB
redis                 latest    7614ae9453d1   4 months ago     113MB
mysql                 latest    3218b38490ce   4 months ago     516MB
hello-world           latest    feb5d9fea6a5   7 months ago     13.3kB
centos                latest    5d0da3dc9764   7 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   13 months ago    79.1MB

# 6.运行自己制作的镜像
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker run -it mytomcat:1.0 /bin/bash
root@bc1b6aac1bea:/usr/local/tomcat# cd webapps	
root@bc1b6aac1bea:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager
root@bc1b6aac1bea:/usr/local/tomcat/webapps# cd ..
root@bc1b6aac1bea:/usr/local/tomcat# exit

9. Container data volume

9.1. Introduction to data volumes

Docker can package applications and operating environments into an image.

If the database MySQL is deployed in docker and docker is accidentally deleted, wouldn't it mean that the database is deleted and the database is gone? So how to save data in docker?

  1. Generate a new image through docker commit, save some data, and commit into a new image every time the data changes, which is a bit too complicated;
  2. Can the data in the container be stored locally and persistently?

This is the technology of data volumes. By mounting, the directory in the container is mounted on Linux, so that the data can be synchronized to the local and facilitated for persistent storage. After all, data is priceless.

The design purpose of volumes is data persistence, which is completely independent of the life cycle of the container. Therefore, Docker will not delete its mounted data volume when the container is deleted.

Data volume: realizes persistence and synchronization operations of containers, and can also realize data sharing between containers!

9.2. Basic use

Method 1: Mount using the command -v

# docker run -it -v 主机目录:容器内目录		# 路径映射
# docker run -it -p 主机端口:容器端口		 # 端口映射

# 1.查看home目录下的目录结构
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# cd home
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# ls
kevin  test.java
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# cd ..

# 2. 运行centos,并将centos的home目录挂载到虚拟机home目录的testcentos下 
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker run -it -v /home/testcentos:/home centos /bin/bash
[root@fc3e6a5f86ad /]# 

# 3. 新开窗口连接,查看虚拟机的home目录,发现testcentos目录
[root@iZ2ze1dg1xkfc3i15ybixmZ /]# cd home
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# ls
redis  test1.java  testcentos  test.java  www
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# 

内部查看挂载信息
[root@iZ2ze1dg1xkfc3i15ybixmZ testcentos]# docker inspect fc3e6a5f86ad
挂载信息:显示已经将/home 目录挂载到 /home/testcentos

Insert image description here

Create a new test.java file in the home directory of the container. The file can be viewed synchronously in the directory of the virtual machine testcentos.

Insert image description here

Then, make modifications in the file corresponding to the virtual machine to see if the modified file can be displayed normally in the container.

# 1. 先停止容器并退出,
[root@fc3e6a5f86ad /]# exit
exit
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# clear
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND        CREATED        STATUS        PORTS                    NAMES
3cad49e5d24c   portainer/portainer   "/portainer"   25 hours ago   Up 11 hours   0.0.0.0:8088->9000/tcp   jovial_perlman
# 2.在主机中修改testcentos目录下的test.java
# vim操作,i进入插入模式; :wq保存并退出
[root@iZ2ze1dg1xkfc3i15ybixmZ testcentos]# vim test.java

# 3.重新启动刚才的centos,进入容器里面查看test.java发现内容已经被修改成功
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker start fc3e6a5f86ad
fc3e6a5f86ad
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker attach fc3e6a5f86ad
[root@fc3e6a5f86ad /]# cd home
[root@fc3e6a5f86ad home]# ls
test.java
[root@fc3e6a5f86ad home]# cat test.java
hello, Linux update			#成功显示修改信息

The mounting and binding of data volumes is a two-way process. In the future, the content of the mount binding part can be modified only on the virtual machine without entering the container!

9.3. Exercise - Install and deploy MySQL

For persistence of MySQL data, the data directory in MySQL can be mounted to the virtual machine through mounting.

  1. Download mysql image
# docker pull 命令下载 mysql 5.7版本
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
# 查看本地镜像中是否下载了mysql 5.7
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
mytomcat              1.0       fae635bb688d   2 days ago      684MB
elasticsearch         8.2.0     f75ee9faf718   3 weeks ago     1.21GB
nginx                 latest    605c77e624dd   4 months ago    141MB
tomcat                latest    fb5657adc892   4 months ago    680MB
redis                 latest    7614ae9453d1   4 months ago    113MB
mysql                 5.7       c20987f18b13   4 months ago    448MB
hello-world           latest    feb5d9fea6a5   7 months ago    13.3kB
centos                latest    5d0da3dc9764   8 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   14 months ago   79.1MB
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]#
  1. Run the image and complete the path mounting
# 运行镜像
-d 后台运行
--name 指定名字
-v 主机目录:容器内目录	# 需要挂载多个路径时,每个挂载用一个-v即可
-p 主机端口:容器内端口	
-e MYSQL_ROOT_PASSWORD=123454321 初始化mysql的登录密码
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker run -d --name mysql_5_7 -v /home/mysql/config:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123454321 mysql:5.7
  1. After the startup is successful, connect to the MySQL database locally to add a database table, and you can view the corresponding mounting directory in the directory of the virtual machine.

Warm reminder: The default port for command line connection is 3306. After the port mapping is modified, the -P port command needs to be added to the command line.

[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# mysql -h 39.107.241.179 -P 3307 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

# 成功通过命令行进入到数据库

After we delete the mysql container in the virtual machine, the directory originally mounted in the virtual machine is still retained, ensuring that no data is lost.

9.4. Anonymous mounting and named mounting

9.4.1. Anonymous mounting

Anonymous mounting, literally means that when mounting a data volume, the path to be mounted on the host is not specified, so the mounting will be at the default path of the host: /var/lib/docker/volumes/ Randomly name a folder as the mount path.

Test: Run the nginx container, and the path in the anonymously mounted container is: /etc/nginx

# -P(大写P) 随机映射端口
docker run -d -P --name nginx01 -v /etc/nginx nginx
# docker run 后台运行nginx,随机映射端口,匿名挂在
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# docker run -d -P -v /etc/nginx nginx
f7d28a8dc9ea221c0a8d192d8079b28166151d11890ac432b75b0389b4005f5f
# 运行成功之后,进入home目录
[root@iZ2ze1dg1xkfc3i15ybixmZ ~]# cd home
# 通过docker volume ls 查看所有的volumes
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# docker volume ls
DRIVER    VOLUME NAME
local     e0d7fd0fa4fea35d21a8ee229d058812ff1607e6e81c1bb7f390b2f05fa4f88e
local     e46617b607b5c2cd660cbe1b222e7b7b083787af73e8bcac18a4115c9c698971
local     f4b46eeb747bf12180df6eb2a523b0acdcb48725c9137aab594fd0d9719a40df
# 看到volums的名字为随机生成的,此为匿名挂载
9.4.2. Named mount

Named mount, as the name suggests, is to specify the volume name when mounting. Different from the specified path mount, the path where the volume name specified in the named mount is located is the path under the data volume by default.

Test: Restart an nginx container, mount it with a name, and rename it to nginx02

# 随机指定端口,重命名为nginx02, 具名挂载,卷名设置为juming-nginx
docker run -d -P -name nginx02 -v juming-nginx:/etc/nginx nginx
# 重新启动一个具名挂载的nginx
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
206217bd55b69753a7788d1a9872bab2500e146e2c962394da3ddd0b600d9fd3
# 查看卷列表,可以看到有名为 juming-nginx的卷信息
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# docker volume ls
DRIVER    VOLUME NAME
local     e0d7fd0fa4fea35d21a8ee229d058812ff1607e6e81c1bb7f390b2f05fa4f88e
local     e46617b607b5c2cd660cbe1b222e7b7b083787af73e8bcac18a4115c9c698971
local     f4b46eeb747bf12180df6eb2a523b0acdcb48725c9137aab594fd0d9719a40df
local     juming-nginx
# 通过docker volume inspect 卷名   查看指定卷的相关信息
[root@iZ2ze1dg1xkfc3i15ybixmZ home]# docker volume inspect juming-nginx
[
    {
    
    
        "CreatedAt": "2022-05-15T23:49:34+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]
# 查询出来的信息为:MountPoint为卷的默认挂载路径

You can see that the mounting path of the specified juming-nginxde volume in the host data volume is: /var/lib/docker/volumes/volume name/_data;

All Docker data volumes are in the /var/lib/docker/volumes/ directory by default

9.4.3. Differences

How to distinguish between anonymous mount and named mount?

-v 容器内路径    			# 匿名挂载
-v 卷名:容器内路径			   # 具名挂载
-v /宿主机路径:容器内路径		 # 指定路径挂载

To sum up, the data volume we mounted can be easily found through named mounting. It is recommended to use named mounting.

One additional point: Instructions on ro and rw when mounting

  • ro readonly is read-only. When set to read-only permission, it means that the corresponding path in the container cannot be operated and can only be operated through the path of the host.
  • rw readwrite can read and write, the default permission for volume mounting.

Expression:

# ro(只读)的表示
docker run -d -P -name nginx02 -v juming-nginx:/etc/nginx:ro nginx
#rw(可读可写)的表示
docker run -d -P -name nginx02 -v juming-nginx:/etc/nginx:rw nginx

9.5. Create an image using DockerFile method

Dockerfile is a build file used to build a docker image and is a script command.

The second way to create an image: create an image through dockerfile

  1. First, create a new docker-test-volume folder in the home directory, create a new dockerfile1 file in it, and write the following script:
FROM centos

VOLUME ["volume01", "volume02"]

CMD echo "----end----"

CMD /bin/bash 
  1. After creation, the docker build method executes the build image

    # docker build -f dockerfile的全路径 -t 镜像名:tag .
    [root@kevinDing docker_test_volume]# docker build -f /home/docker_test_volume/dockerfile1 -t kevin/mycentos:1.0 .
    Sending build context to Docker daemon  2.048kB
    Step 1/4 : FROM centos
     ---> 5d0da3dc9764
    Step 2/4 : VOLUME ["volume01", "volume02"]
     ---> Running in b860235245ea
    Removing intermediate container b860235245ea
     ---> 0bc3ac498d5f
    Step 3/4 : CMD echo "----end----"
     ---> Running in 324159180d29
    Removing intermediate container 324159180d29
     ---> 0a3ee43af21b
    Step 4/4 : CMD /bin/bash
     ---> Running in b3263513fb82
    Removing intermediate container b3263513fb82
     ---> f520432b291f
    Successfully built f520432b291f
    Successfully tagged kevin/mycentos:1.0
    [root@kevinDing docker_test_volume]# docker images
    REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
    kevin/mycentos   1.0       f520432b291f   6 seconds ago   231MB
    hello-world      latest    feb5d9fea6a5   7 months ago    13.3kB
    centos           latest    5d0da3dc9764   8 months ago    231MB
    [root@kevinDing docker_test_volume]# 
    
    

Insert image description here

  1. Run the image you built and view the directory information within the image

    [root@kevinDing docker_test_volume]# docker run -it f520432b291f /bin/bash
    # 查看列表信息,可以看到最后两个文件为挂载的目录volume01和volume02
    [root@8ebf26cff30f /]# ls -l
    total 56
    lrwxrwxrwx  1 root root    7 Nov  3  2020 bin -> usr/bin
    drwxr-xr-x  5 root root  360 May 20 15:17 dev
    drwxr-xr-x  1 root root 4096 May 20 15:17 etc
    drwxr-xr-x  2 root root 4096 Nov  3  2020 home
    lrwxrwxrwx  1 root root    7 Nov  3  2020 lib -> usr/lib
    lrwxrwxrwx  1 root root    9 Nov  3  2020 lib64 -> usr/lib64
    drwx------  2 root root 4096 Sep 15  2021 lost+found
    drwxr-xr-x  2 root root 4096 Nov  3  2020 media
    drwxr-xr-x  2 root root 4096 Nov  3  2020 mnt
    drwxr-xr-x  2 root root 4096 Nov  3  2020 opt
    dr-xr-xr-x 94 root root    0 May 20 15:17 proc
    dr-xr-x---  2 root root 4096 Sep 15  2021 root
    drwxr-xr-x 11 root root 4096 Sep 15  2021 run
    lrwxrwxrwx  1 root root    8 Nov  3  2020 sbin -> usr/sbin
    drwxr-xr-x  2 root root 4096 Nov  3  2020 srv
    dr-xr-xr-x 13 root root    0 May 17 01:11 sys
    drwxrwxrwt  7 root root 4096 Sep 15  2021 tmp
    drwxr-xr-x 12 root root 4096 Sep 15  2021 usr
    drwxr-xr-x 20 root root 4096 Sep 15  2021 var
    drwxr-xr-x  2 root root 4096 May 20 15:17 volume01
    drwxr-xr-x  2 root root 4096 May 20 15:17 volume02
    [root@8ebf26cff30f /]# 
    
  2. Open another connection and view the internal information of the image through docker inspect

    # 新开连接,查看运行的镜像
    [root@kevinDing /]# docker ps
    CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS         PORTS     NAMES
    8ebf26cff30f   f520432b291f   "/bin/bash"   2 minutes ago   Up 2 minutes             suspicious_stonebraker
    # 进入到镜像内部查看
    [root@kevinDing /]# docker inspect f520432b291f
    
    [
        {
          
          
            "Id": "8ebf26cff30fd0eb9c75e144eb7c8b18bd07def9faa1d413fa12ad7f5c8efc90",
            "Created": "2022-05-20T15:17:29.740198902Z",
            "Path": "/bin/bash",
            "Args": [],
           ......   中间内容省略
            "Mounts": [
                {
          
          
                    "Type": "volume",
                    "Name": "de516c45849d34553a8e9ee1c76ca4c3949aeebdfd9cb07d7a08e4d8f78a3273",
                    "Source": "/var/lib/docker/volumes/de516c45849d34553a8e9ee1c76ca4c3949aeebdfd9cb07d7a08e4d8f78a3273/_data",
                    "Destination": "volume01",
                    "Driver": "local",
                    "Mode": "",
                    "RW": true,
                    "Propagation": ""
                },
                {
          
          
                    "Type": "volume",
                    "Name": "7f18328f873a7760294840e4fef2e9688d2303c071a695736fc0b41a802abb73",
                    "Source": "/var/lib/docker/volumes/7f18328f873a7760294840e4fef2e9688d2303c071a695736fc0b41a802abb73/_data",
                    "Destination": "volume02",
                    "Driver": "local",
                    "Mode": "",
                    "RW": true,
                    "Propagation": ""
                }
            ]
        }
    ]
    
    
    
    

Insert image description here

  1. Verify whether the mount is successful

    Enter the mounting directory volume01 inside the image, create a container_test.txt file, and check whether the file is generated in the mounting path of volume01.

    [root@8ebf26cff30f /]# cd volume01
    [root@8ebf26cff30f volume01]# touch container_test.txt
    [root@8ebf26cff30f volume01]# ls
    container_test.txt
    [root@8ebf26cff30f volume01]# 
    # 在新开的连接中,进入到volume01的挂载路径中,可以查看到创建的文件
    [root@kevinDing volumes]# cd /var/lib/docker/volumes/de516c45849d34553a8e9ee1c76ca4c3949aeebdfd9cb07d7a08e4d8f78a3273/_data
    [root@kevinDing _data]# ls
    container_test.txt
    [root@kevinDing _data]# 
    

Insert image description here

9.6. Container data volume

Container data volume refers to establishing a data volume to achieve data synchronization between containers.

Insert image description here

Start the centos image created above, start centos01centos02, and centos01 is the parent container:

[root@kevinDing ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
kevin/mycentos   1.0       f520432b291f   52 minutes ago   231MB
hello-world      latest    feb5d9fea6a5   7 months ago     13.3kB
centos           latest    5d0da3dc9764   8 months ago     231MB
# 启动一个centos01容器
[root@kevinDing ~]# docker run -it --name centos01 kevin/mycentos:1.0
# ctrl+P+Q退出当前容器
# 再启动一个centos02容器, --volumes-from centos01
[root@kevinDing ~]# docker run -it --name centos02 --volumes-from centos01 kevin/mycentos:1.0

After opening a connection, enter two containers to view the content:

[root@kevinDing ~]# docker ps
CONTAINER ID   IMAGE                COMMAND                  CS         PORTS     NAMES
5b0deb75f776   kevin/mycentos:1.0   "/bin/sh -c /bin/bash"   4minutes             centos02
b8dcce0bcf25   kevin/mycentos:1.0   "/bin/sh -c /bin/bash"   5minutes             centos01
[root@kevinDing ~]# docker attach b8dcce0bcf25
[root@b8dcce0bcf25 /]# ls -l
total 56
lrwxrwxrwx  1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x  5 root root  360 May 20 16:04 dev
drwxr-xr-x  1 root root 4096 May 20 16:04 etc
drwxr-xr-x  2 root root 4096 Nov  3  2020 home
lrwxrwxrwx  1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx  1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------  2 root root 4096 Sep 15  2021 lost+found
drwxr-xr-x  2 root root 4096 Nov  3  2020 media
drwxr-xr-x  2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x  2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 97 root root    0 May 20 16:04 proc
dr-xr-x---  2 root root 4096 Sep 15  2021 root
drwxr-xr-x 11 root root 4096 Sep 15  2021 run
lrwxrwxrwx  1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x  2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x 13 root root    0 May 17 01:11 sys
drwxrwxrwt  7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x 12 root root 4096 Sep 15  2021 usr
drwxr-xr-x 20 root root 4096 Sep 15  2021 var
drwxr-xr-x  2 root root 4096 May 20 16:04 volume01
drwxr-xr-x  2 root root 4096 May 20 16:04 volume02
[root@b8dcce0bcf25 /]# cd volume01
[root@b8dcce0bcf25 volume01]# ls
[root@b8dcce0bcf25 volume01]# touch test01.txt
[root@b8dcce0bcf25 volume01]# ls
test01.txt
[root@b8dcce0bcf25 volume01]# 

Insert image description here
Insert image description here

If the container centos01 is stopped and deleted, the data in the container centos02 will not be affected. This is equivalent to a two-way copy of the mounted directory when performing volumes-from, realizing data sharing between containers.

Similarly, when sharing data between multiple MySQL databases and configuration files, the operations for centos01 and centos02 above are similar. First, start mySQL01 and create a data volume. Then use –volumes-from to mount the container data volume to another MySQL container:

# 挂载第一个MySQL容器 匿名挂载
[root@KevinDing ~]# docker run -d --name mysql01 -p 3307:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123454321 mysql:5.7

# 挂载第二个MySQL,因为是挂载到mysql01,无需再进行-v的匿名挂载了
[root@KevinDing ~]# docker run -d --name mysql02 --volumes-from mysql01 -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123454321 mysql:5.7

This enables data synchronization between two MySQL containers.

Configuration information is transferred between containers, and the life cycle of a data volume container continues until no more containers are used.
Once the contents of the parent container are persisted locally, the local data will not be lost without deletion.

Guess you like

Origin blog.csdn.net/weixin_43155804/article/details/124892975