Data sharing of volume volumes between containers without a specified mount point in Docker
A background
In the process of actual use, we may often encounter the situation of data sharing between containers, how to deal with it? With some options in the docker command, we can complete the data sharing between containers.
2 Lab step 2.1 create a container
Container 1: gysl-1
[root@dev] # docker run-it-- rm-- name gysl-1-v / data-1 alpine
Container 2: gysl-2
[root@dev ~] # docker run-it-- rm-- volumes-from gysl-1-- name gysl-2 alpine2.2 verifies data sharing
Create a file in the container gysl-1: gysl-1.txt
/ # cd data-1//data-1 # touch gysl-1.txt
Create a file in the container gysl-2: gysl-2.txt
/ # cd data-1//data-1 # touch gysl-2.txt
View it in two containers:
/ data-1 # ls-lhtotal 0 RWMurray Rafael-1 root root 0 Jan 10 18:45 gysl-1.txt-rw-r--r-- 1 root root 0 Jan 10 18:47 gysl-2.txt
The contents in the data-1 directory of the two containers are exactly the same.
Three summaries
3.1 when the volume of a container is shared by other containers, there is no need for other containers to create a shared directory, and the shared directory will be automatically created in other containers, which is the same as the directory name of the shared container.
3.2 the volume of a container can be shared by multiple containers at the same time.
When the container is deleted, the volume will not be deleted automatically. If the data will not be used again, you can delete the obsolete volume manually, as follows:
[root@dev ~] # docker volume lsDRIVER VOLUME NAMElocal 8126b3ad828a9a7e29ec04f4d7a1901be5e40ca6157fde62dca3421322e5de7alocal bf80e1eb66685161cb6bf6943079de4a68a7bc3db3bba241347ed051fe59fc46 [root@dev ~] # docker volume pruneWARNING! This will remove all volumes not used by at least one container.Are you sure you want to continue? [y/N] yTotal reclaimed space: 0 B
You can also:
Docker volume rm volume_name
You can also (force the deletion of volume while deleting the container):
Docker rm-vf container_name