Art is long, life is long
introduce
A data volume is a special directory that can be used by one or more containers, bypassing UFS and providing a number of useful features:
Data volume
It can be shared and reused between containers- right
Data volume
The changes will take effect immediately - right
Data volume
The update does not affect the mirror Data volume
The default persists even if the container is deleted
Note: Using a data volume is similar to mounting a directory or file under Linux. Files in the directory specified as the mount point in the image are copied to the data volume (only when the data volume is empty).
Create a data volume
[root@jiangwang /]# docker volume create my-web
my-web
Copy the code
View all data volumes
[root@jiangwang /]# docker volume ls
DRIVER VOLUME NAME
local my-web
Copy the code
Run the following command on the host to view information about the specified data volume
[root@jiangwang /]# docker volume inspect my-web
[
{
"CreatedAt": "2021-03-10T14:53:42+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-web/_data",
"Name": "my-web",
"Options": {},
"Scope": "local"
}
]
Copy the code
Mount a host directory as a data volume
#Create a new folder under the home directory of the hosttest
#Command docker run it -v Host directory: the directory in the container
#Put the home folder on the host undertestIs bound to the home folder in the container[root@jiangwang home]# docker run it -v /home/test:/home centos /bin/bash [root@3cf5fe5b9f86 /]# CD /home/# Folder [root@3cf5fe5b9f86 home]# ls [root@3cf5fe5b9f86 home]# [root@jiangwang home]# ls f2 f3 Jiangwang test test.java WWW [root@jiangwang home]# CD test # [root@jiangwang test]# ls [root@jiangwang test]#Copy the code
Now, create a test. Java file under the container’s home folder and check to see if there is a test. Java file under the host’s home/test/ folder, as shown below.
View details about data volumes
[root@jiangwang /]# docker inspect 3cf5fe5b9f86
Copy the code
The Mounts host directory configuration information is under the Mounts Key:
Deleting a Data Volume
[root@jiangwang /]# docker volume rm my-web
Copy the code
The data volume is designed to persist data, and its life cycle is independent of the container. Docker does not automatically delete the data volume after the container is deleted, and there is no garbage collection mechanism to deal with the data volume without any container reference. If you need to remove the data volume while deleting the container. You can use docker rm -v to delete containers.
An ownerless data volume may occupy a lot of space. To clean it up, use the following command
[root@jiangwang /]# docker volume prune
Copy the code
Conclusion:
Data volume: it can persist and synchronize the data of containers, share the data between containers, and synchronize the data between containers and host machines.