[Docker article] Deep understanding of container data volume, anonymous mount and named mount, data volume container

data container volume

1. What is a container data volume

We all know that through Docker, our software runtime environment and software developed based on the runtime environment can be packaged into images, and the image operation is accompanied by containers, and the life cycle of internal data files is also the same as the life cycle of the container. But in our actual application, we want to be able to persist some data.

For example, if you install a MySQL, and you delete the container, it is equivalent to deleting the database and running away. This TM is too ridiculous!

The data generated by the Docker container, if a new image is not generated by docker commit, so that the data is saved as part of the image, then when the container is deleted, the data will naturally be gone! This will not work!

So we hope to have a technology that can realize data sharing. So the container roll technology was born.

To be able to save data in Docker, we can use volumes! Let the data mount to our local (disk that can persist data)! This way data will not be lost due to container deletion!

insert image description here

Function :

A volume is a directory or file that exists in one or more containers and is mounted to the container by docker, but it does not belong to the Union File System, so it can bypass the Union File System and provide some features for persistent storage or sharing of data.

Volumes are designed to persist data and are completely independent of the container's life cycle, so Docker will not delete the mounted data volume when the container is deleted.

Features :

  1. Data volumes to share or reuse data between containers
  2. Changes in the volume can take effect directly
  3. Changes in data volumes will not be included in mirrored updates
  4. The lifecycle of a data volume lasts until no containers use it

To sum up, the persistence of container data and the data sharing between containers are realized through data volumes.

2. Using data volumes

Now that we know that there are many advantages to using data volumes, how do we use them?

Method 1: We use the command to add directly.

Mount :

# 命令
docker run -it -v 宿主机绝对路径目录:容器内目录 镜像名

# 测试
[root@jiangnan ~]# docker run -it -v /home:/home centos /bin/bash

-v is to mount our host and container.

We can docker inspect 容器idcheck whether the mount is successful, mainly looking at the Mounts section.

"Mounts": [
            {
    
    
                "Type": "bind",
                "Source": "/home",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

Source is the path to the corresponding virtual machine. Destination is the path within the corresponding container.

Test :

We first create a new hello.txt file in the /home directory of the virtual machine to see if it can be synchronized to the /home directory in the container.

insert image description here

Discovery: Files created on the host can also be seen in the container.

We modify the file inside the container. See if the files on the host computer can also be modified.

insert image description here

Discovery: Modify the files in the container, the files of the host are also modified, and the data is synchronized in both directions .

As we said above, the data volume mounting is to achieve data persistence and prevent the database from being deleted and run, so can it be done?

We stop the container.

insert image description here

Found: Although the container is stopped, the local hello.txt still exists. Data persistence is achieved.

At this time, if we modify the local file again, when we restart the container, we find that the files in the container are kept in sync with our locally modified files, which truly realizes data synchronization and sharing.

insert image description here

Method 2: Use Dockerfile

Here is a simple Dockerfile for you, which we will talk about in a later blog post.

  1. We now create a new folder /tomcat-volume folder in the host /home directory
[root@jiangnan home]# mkdir tomcat-volume
[root@jiangnan home]# ll
total 12
-rw-r--r-- 1 root root   12 Feb 22 22:53 hello.txt
drwxr-xr-x 4 root root 4096 Feb 22 23:16 tomcat
drwxr-xr-x 2 root root 4096 Feb 23 19:25 tomcat-volume   # 新建一个tomcat-volume文件夹
[root@jiangnan home]# 
  1. Write a Dockerfile
[root@jiangnan ~]# cd /home/tomcat-volume/
[root@jiangnan tomcat-volume]# vi Dockerfile
[root@jiangnan tomcat-volume]# cat Dockerfile 
FROM centos
VOLUME ["/volume1","/volume2"]
CMD echo "-------end------"
CMD /bin/bash
[root@jiangnan tomcat-volume]# 

Use the VOLUME command in the DockerFile file to add one or more data volumes to the image

  1. Generate an image named mycentos

Note that the image name cannot have uppercase

[root@jiangnan tomcat-volume]# docker build -f /home/tomcat-volume/Dockerfile -t mycentos . 
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["/volume1","/volume2"]
 ---> Running in a9dcdbdb111e
Removing intermediate container a9dcdbdb111e
 ---> 6811c813ce47
Step 3/4 : CMD echo "-------end------"
 ---> Running in 92c7e1ef5987
Removing intermediate container 92c7e1ef5987
 ---> e58a5176b91d
Step 4/4 : CMD /bin/bash
 ---> Running in 3f1310167f22
Removing intermediate container 3f1310167f22
 ---> 91f16e4a921e
Successfully built 91f16e4a921e
Successfully tagged mycentos:latest
[root@jiangnan tomcat-volume]# 

-f: Specify Dockerfile. -t: Name the image. There is a command at the end that .cannot be omitted.

insert image description here

The mirroring was created successfully.

  1. Start the image you just made and view the directory structure
[root@jiangnan tomcat-volume]# docker run -it 91f16e4a921e /bin/bash
[root@1fabdfdf4803 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Feb 23 11:38 dev
drwxr-xr-x   1 root root 4096 Feb 23 11:38 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
drwxr-xr-x  12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x  20 root root 4096 Sep 15 14:17 var
drwxr-xr-x   2 root root 4096 Feb 23 11:38 volume1  # 数据卷目录
drwxr-xr-x   2 root root 4096 Feb 23 11:38 volume2  # 数据卷目录
[root@1fabdfdf4803 /]# 
  1. View the local mount directory
[root@jiangnan home]# docker inspect 1fabdfdf4803

insert image description here

This also allows for mounting. New files are visible both locally and in the container.

3. Actual combat

We all know that the tomcat deployment project file needs to be placed under webapps to be accessible, but do we need to start tomcat every time the file is updated? Obviously not, we just need to hang the webapps directory in a local directory, put the required files in it, and it will be automatically synchronized to the webapps directory of tomcat.

[root@jiangnan home]# docker run -d -v /home/tomcat/webapps:/usr/local/tomcat/webapps -p 8081:8080 --name tomcat01 tomcat

After startup, I put the prepared project in the /home/tomcat/webappsdirectory of the host machine.

insert image description here

Discovery: It does exist in the container.

Let's visit and see the effect. I used port 8081 to map the port 8080 of the container, so I need to use port 8081 to access.

insert image description here

no problem.

In practical applications, we can also mount some configuration files, which is more convenient to modify.

How to share data between containers ?

We also mentioned data sharing between containers above. How to do this?

We start another tomcat03, which is also mounted locally /home/tomcat/webapps. Expose port 8082.

[root@jiangnan webapps]# docker run -d -v /home/tomcat/webapps:/usr/local/tomcat/webapps -p 8082:8080 --name tomcat03 tomcat
9aacff8773bc1ee92acc2c598fd497cde18e9c44192a9942906ae16c5e0ead2e
[root@jiangnan webapps]# 

Direct access without doing anything.

insert image description here
insert image description here

No problem either.

Because the two tomcats are mounted to the same local directory, they both share the resources in the local directory. This method indirectly realizes data sharing between containers.

4. Anonymous mounts and named mounts

# 匿名挂载
-v 容器内路径
docker run -d -P --name nginx01 -v /etc/nginx nginx

# 匿名挂载的缺点,就是不好维护,通常使用命令 docker volume维护
docker volume ls

[root@jiangnan webapps]# docker run -d -p 80:80 --name nginx01 -v /etc/nginx nginx
d270788d32058ffbd9dbbd9a98133f42b05dbf3881d25b8d482d36c12c0279d5
[root@jiangnan webapps]# docker volume ls
DRIVER    VOLUME NAME
local     4b595f9bdc43c312f982c594a3f3087920e08d53b9f9e090779509d14f0f4638

# 查看挂载的路径
[root@jiangnan webapps]# docker volume inspect 4b595f9bdc43c312f982c594a3f3087920e08d53b9f9e090779509d14f0f4638
[
    {
    
    
        "CreatedAt": "2022-02-22T23:49:12+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/4b595f9bdc43c312f982c594a3f3087920e08d53b9f9e090779509d14f0f4638/_data",
        "Name": "4b595f9bdc43c312f982c594a3f3087920e08d53b9f9e090779509d14f0f4638",
        "Options": null,
        "Scope": "local"
    }
]
[root@jiangnan webapps]# 
# 具名挂载
-v 卷名:/容器内路径
docker run -d -P --name nginx02 -v nginxconfig:/etc/nginx nginx

[root@jiangnan webapps]# docker run -d -p 81:80 --name nginx02 -v nginxconfig:/etc/nginx nginx
6e3a6b44c49c5b656c9a0dc838c66c2614537b4b00503b8a567140d1fde4adfb

# 查看挂载的路径
[root@jiangnan webapps]# docker volume inspect nginxconfig
[
    {
    
    
        "CreatedAt": "2022-02-22T23:50:48+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/nginxconfig/_data",
        "Name": "nginxconfig",
        "Options": null,
        "Scope": "local"
    }
]
[root@jiangnan webapps]# 

Named and anonymous is to give our mounted volume a name. Obviously named mounts are more convenient.

How to judge that the volume name is mounted instead of the local directory name?

is/begins is the volume name, is/begins is the directory name

Change the read and write permissions of the file
ro: readonly
rw: readwrite

Specify the read and write permissions of the container to the content we mount

docker run -d -p 81:80 --name nginx02 -v nginxconfig:/etc/nginx:ro nginx
docker run -d -p 81:80 --name nginx02 -v nginxconfig:/etc/nginx:rw nginx  

5. Data Volume Container

The named container mounts the data volume, and other containers realize data sharing by mounting this (parent container), and the container that mounts the data volume is called the data volume container.

In the previous example, we created the image mycentos, and we used this as a template to run the containers mycentos01, mycentos02, mycentos03

Let's test, transfer sharing between containers

  1. Start the parent container mycentos01

Background process.

[root@jiangnan tomcat-volume]# docker run -d -it --name mycentos01 mycentos
[root@9ba991b24352 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Feb 23 11:50 dev
drwxr-xr-x   1 root root 4096 Feb 23 11:50 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
drwxr-xr-x  12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x  20 root root 4096 Sep 15 14:17 var
drwxr-xr-x   2 root root 4096 Feb 23 11:50 volume1
drwxr-xr-x   2 root root 4096 Feb 23 11:50 volume2
[root@9ba991b24352 /]# 
  1. Create mycentos02 and mycentos03 and let them inherit mycentos01

--volumes-from

[root@jiangnan tomcat-volume]# docker run -d -it --name mycentos02 --volumes-from mycentos01 centos
c804e323d49c7203809069ed413c692e637adc02f3797957f1b6537c7d1bbf5c

[root@jiangnan tomcat-volume]# docker run -d -it --name mycentos03 --volumes-from mycentos01 centos
2306661ebddd801550a20765ce7dc123cdff7d60cfebe3dc6287a8d659cf58cd
[root@jiangnan tomcat-volume]# 
  1. Now we go into mycentos03 and create the file.
[root@jiangnan tomcat-volume]# docker exec -it 2306661ebddd /bin/bash
[root@2306661ebddd /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume1  volume2
[root@2306661ebddd /]# cd volume1
[root@2306661ebddd volume1]# ls
[root@2306661ebddd volume1]# touch mycentos03.txt
[root@2306661ebddd volume1]# 

Then we enter mycentos01 and mycentos02, and we can also see the files created by mycentos03.
insert image description here

In fact, data sharing is realized, and files or modifications created in any container can be updated synchronously in other associated containers.

Even if some of these containers are deleted, other containers can still see the updated status of the files.

Conclusion :
The transfer of configuration information between containers, the life cycle of the data volume continues until no container uses it.
The files stored on this computer will always be preserved!

6. Summary

  1. Container data volumes are actually technologies that implement container data persistence operations.
  2. Persistence of container data and data sharing between containers are realized through data volumes.
  3. When starting the container, you can mount the container volume through the command, or you can add it in the Dockerfile.
  4. is/begins is the volume name, is/begins is the directory name. Named mounts have names, anonymous mounts have no names.
  5. docker volume lsUsed to view and maintain data volumes.
  6. The container that mounts the data volume is called the data volume container.
  7. The essence of data sharing between containers is to mount to a local directory to realize data sharing .
  8. The lifecycle of a data volume lasts until no containers use it.
  9. The files stored in this unit are always retained.

insert image description here
My WeChat public account has been opened first, you can follow me, and the articles will be synchronized later for easy viewing.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45842494/article/details/123098436
Recommended