What is a container data volumeThe container data volume is the mount of the directory. The directory of our container is mounted on the host machine, so as to realize the file sharing function between the host machine and the container. Why do we need container data volumes?The idea of Docker is to package the application and environment into an image; but what about the data? Let alone the database, a project will definitely generate a lot of logs during operation. These logs are very important to developers, because with these logs, we can know what problems occurred during the operation and then troubleshoot. However, in the container, each time the project is updated and iterated, the container will be deleted and replaced with a new image. In this case, if you want to save these log files, if you copy them to the host machine every time, the workload will be a bit large, and if the log files are too large, the copying work will also be very time-consuming and labor-intensive. So at this time, you need to use the container data volume function. To put it simply, this function is very simple, which is to open up the file sharing function between the host and the container. The data files generated in the docker container will be synchronized to the host machine in real time. On the contrary, the files generated by the host machine will also be synchronized to the container. In this way, a two-way transmission pipeline is opened up. After data sharing is implemented between containers, there is no distinction between main containers and sub-containers, because there is only one copy of the shared data, which is stored on the host machine. Deleting any container will not affect the data synchronization of other containers. use Using container data volumes is very simple. Just add the
After running the above command, the corresponding directories will be automatically created on the container and the host, and any files created or modified in the directories will be automatically synchronized. How to Check Whether a Data Volume is Used To check whether a container uses the container data volume function, you can use the
After executing the above command, a lot of formatted json strings will be printed. At this time, we find that the item with key "Mounts": [ { "Type": "bind", "Source": "/root/dockerContainer", # Directory of the host machine "Destination": "/text", # Directory of the container "Mode": "", "RW": true, # RW is readable and writable; ro is read-only and can only modify files on the host machine; "Propagation": "rprivate" } ], Named and anonymous mountsMount by specifying a pathIn the above examples, we use the specified path mount, that is, configure the host path and the container path;
Named mountMount to a directory with the specified name; # /xxx is a directory, xxx is a volume name, the one without a slash 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 Let's test it. First, create a container and mount the directory.
Use the inspect command to view container information
Find "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 MountAnonymous mounting means that there is only a container directory, but no host directory, so the generated directory is a long encrypted string. Generally, anonymous mounting is not recommended; encrypted strings make it difficult to find.
Come, test it, first create a container and mount the directory
Use the inspect command to view container information
Find "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-fromThere is a scenario where we need container A and container B to share data. That is, I want to see the content modified in container A on container B. So how should this function be achieved? Then you can use the data volume container function, which can also synchronize data between multiple containers, not just two containers. 1. Create the first container centos_1 and mount the /data/centos directory on the host. The directories of the host and the container are both
2. Create a second container and bind it to the first container;
3. Now we create a third container and bind it to the second container centos_2
Next, we create a file in the /data/centos directory in each container
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 achieve data synchronization between containers. [root@259efdc362b4 centos]# ls centos_1.java centos_2.java centos_3.java main.java This is the end of this article about docker container data volumes - named mounts and anonymous mounts. For more relevant docker container data volumes content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: CSS to achieve text on the background image
>>: Summary of knowledge points about events module in Node.js
Although Microsoft provides T4 templates, I find ...
①. How to use the alias (CNAME) record: In the do...
The Linux stream editor is a useful way to run sc...
Table of contents Preface Basic Usage grammar Err...
Table of contents Problem Description Scenario In...
The box model specifies the size of the element b...
1. Start the Docker container Start a new Docker ...
The relationship between Tomcat logs A picture is...
Copy code The code is as follows: wmode parameter...
Example: We use the Python code loop_hello.py as ...
This article shares the specific code of JS to ac...
Preface Semicolons in JavaScript are optional, an...
Some of you may have heard that the order of trav...
hint This plug-in can only be accessed under the ...
Without further ado, these three methods are: ren...