A recent project to use Data Volume, here is a simple record.
Data volumes can be used to store data for Docker applications, or to share data between Docker containers. A data volume is presented to a Docker container in the form of a directory that supports sharing between multiple containers, and the modification does not affect mirroring.
Characteristics:
Data volumes can be shared and reused between containers
Changes to the data volume will take effect immediately
Updates to the data volume do not affect mirroring
The data volume is always present by default, even if the container is deleted
1. Create a data volume
Docker Volume Create Vol1
View all data volumes
Docker Volume LS
Use the following command in the host to view the specified data volume information
Docker Volume Inspect Vol1
[
{
"Driver": "Local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/vol1/_data",
"Name": "Vol1",
"Options": {},
"Scope": "Local"
}
]
2. Using data volumes
(1) Use the "-V in-Container directory" method
Docker run-d-P--name web-v/webapp training/webapp python app.py
This way Docker will mount a local _data directory into the WebApp directory in the container, but after the Web container is deleted, the directory of _data on the host and its contents will remain, and the newly launched container is no longer able to use the directory, which means Existing data cannot be reused automatically.
(2) Use-V to mount a directory/file on a host to a container directory/file
Docker run-d-P 23333:80-v vol1:/store training/webapp python app.py or use--mount source=vol1, Target=/store
The data volume that is created is vol1 to the container's/store directory, which can be understood as a directory mapping, which allows all containers to share host data so that all container data can be affected by changing the host's data source. Also, when you restart the container, you can use the same method to hang the Vol1 directory in the new container again, so that you can achieve the purpose of data persistence.
3. Deleting data volumes
Docker Volume RM vol1
Use the volume of Docker storage volume