Container data volumes - named mount and anonymous mount

Keywords: Operation & Maintenance Docker Container

What is a container data volume

Container data volume is the mounting of the directory. Mount the directory of our container on the host, so as to realize the file sharing function between the host and the container;

Why container data volumes

docker's idea is to package the application and environment into a mirror image; But what about the data? Not to mention the database, a project will certainly generate a large number of logs during operation. These logs are very important to developers. With these logs, we can know what problems have occurred during operation, and then check and find errors;

However, in the container, each update iteration of the project will delete the container and directly replace the new image to run; In this case, if you want to save these log files, if you copy them into the container to the host every time, the workload will be a little heavy, and if the log files are too large, the copying work is also very time-consuming and labor-consuming; So you need to use the container data volume function at this time. To put it bluntly, this function is very simple, that is, to get through the file sharing function between the host and the container; The data files generated in the docker container will be synchronized to the host in real time; On the contrary, the file leaves generated by the host will be synchronized to the container; In this way, the two-way transmission pipeline is opened;

After data sharing is realized between containers, there is no distinction between primary containers and sub containers, because only one copy of the data they share is saved on the host. Deleting any container will not affect the data synchronization of other containers;

use

Using container data volumes is very simple. You only need to add the - v option when running

docker run -d -v Host Directory:Container directory tomcat 

After running the above command, the corresponding directory will be automatically created on the container and host computer, and the files created or modified in the directory will be automatically synchronized;

How do I see if a data volume is used

Check whether a container uses the container data volume function. You can view the details of the container through the inspect function

docker inspect container id/Container name

After executing the above command, a lot of formatted json strings will be printed. At this time, we find the item with the key as Mounts, which is the relevant configuration of the container data volume,

"Mounts": [
    {
        "Type": "bind",  
        "Source": "/root/dockerContainer",  # Directory of the host
        "Destination": "/text",  # Directory of containers
        "Mode": "",
        "RW": true,   # RW is readable and writable; ro is read-only and can only modify the files of the host machine; 
        "Propagation": "rprivate"
    }
],

Named mount and anonymous mount

Specified path mount

In the above example, we all use the specified path mount, that is, both the host path and the container path are configured;

docker run -d -v Host Directory:Container directory tomcat 

Named mount

Mount to a directory with a specified name;

# /xxx is the directory, xxx is the volume name, and the volume without diagonal bar is the volume name
docker run -d -v Volume name:Container directory tomcat 

# Find the directory where the volume name is located
docker volume inspect Volume name

It's no use just saying. Test it. First create a container and mount the directory

docker run -d --name my_tomcat  -v my_folder:/data/my_folder tomcat

Use the inspect command to view container information

docker inspect my_tomcat

Find the Mounts item in the print information, where the directory of Source is the directory of the host; The Destination directory is the mounted container directory;

"Mounts": [
    {
        "Type": "volume",
        "Name": "my_folder",
        "Source": "/var/lib/docker/volumes/my_folder/_data",
        "Destination": "/data/my_folder",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    }
]

Then enter the container

docker exec -it my_tomcat /bin/bash

# This directory has also been created in the container
root@ef94ff8928a1:/data/my_folder# pwd
/data/my_folder

Anonymous mount

Anonymous mount means that there is only container directory and no host directory, so the generated directory is a long encrypted string. Anonymous mount is generally not recommended; Encrypting the string will make it difficult to find;

docker run -d -v \Container directory tomcat 

Let's test it. First create the container and mount the directory

docker run -d --name my_tomcat_2 -v /my_folder_2 tomcat

Use the inspect command to view container information

docker inspect my_tomcat_2

Find the Mounts item in the print information, where the directory of Source is the directory of the host; The Destination directory is the mounted container directory

"Mounts": [
    {
        "Type": "volume",
        "Name": "df4c649772a5ae65716de8ede0607d0776f8c1e2eda1d87b3ec9eaf011b43616",
        "Source": "/var/lib/docker/volumes/df4c649772a5ae65716de8ede0607d0776f8c1e2eda1d87b3ec9eaf011b43616/_data",
        "Destination": "/my_folder_2",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
]

Data sharing between containers -- volumes from

In such a scenario, we need container a and container B to realize data sharing, that is, the content modified on container A. I hope I can see it on container B. how should this function be done? At this time, the data volume container function can be used. The data volume container can also realize the data synchronization function between multiple containers, not just two containers sharing;

1. First create the first container centos_1. Mount the / data/centos directory on the host. The directories of the host and container are / data/centos;

docker run -it --name centos_1 -v /data/centos:/data/centos centos /bin/bash

2. Create a second container and bind it to the first container-- volumes-from centos_ The 1 option is to bind the mount directory of the current container to centos_1 container, so as to realize the data synchronization of multiple containers;

docker run -it --name centos_2 --volumes-from centos_1 centos /bin/bash

3. Now we are creating a third container and binding it to the second container centos_2 up

docker run -it --name centos_3 --volumes-from centos_2 centos /bin/bash

Next, we create a file in the / data/centos directory in each container

  • The main.java file is created in the / data/centos directory of the host computer
  • In centos_1 CentOS is created in the / data/centos directory of the container_ 1. Java file
  • In centos_2 CentOS is created in the / data/centos directory of the container_ 2. Java file
  • In centos_3. CentOS is created in the / data/centos directory of the container_ 3. Java file

Finally, execute the ls command in the / data/centos directory in the four environments, and you can see the files created by all containers. In this way, we can realize the data synchronization between each container;

[root@259efdc362b4 centos]# ls
centos_1.java  centos_2.java  centos_3.java  main.java

Posted by skali on Sun, 31 Oct 2021 18:29:58 -0700