Docker data management
There are two main ways to manage data in containers:
- Data Volumes (Volumes)
- Mounts a host directory (Bind mounts).
Data volume
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 volumes can be shared and reused between containers
- Changes to data volumes take effect immediately
- Data volume updates do not affect mirroring
- The data volume will always exist by default, 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 will be hidden and the mounted data volume can be displayed.
Docker provides two mounting methods, -v and -mount
New Docker users should select the –mount parameter
Experienced Docker users are already familiar with -v or –volume, but the -mount argument is recommended.
Create a data volume
docker volume create my-volume
Copy the code
View information about a specified data volume
docker volume inspect my-volume
Copy the code
Start a container to mount a data volume:
When using the Docker run command, use the –mount flag to mount the data volume to the container.
Create a container called session-Web and load a data volume into the/webApp directory in the container.
Method a #
docker run --name session-web -d -p 8888:8080 --mount source=my-volume,target=/webapp session-web:latest
Method # 2
docker run --name session-web -d -p 8888:8080 -v my-volume:/webapp session-web:latest
Copy the code
Deleting a Data Volume
docker volume rm my-volume
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
docker volume prune
Copy the code
Mount the host directory
Use the –mount flag to specify the directory to mount a localhost to the container
Method a #
docker run --name session-web -d -p 8888:8080 \
-v my-volume:/webapp \
session-web:latest
Method # 2
docker run --name session-web -d -p 8888:8080 \
--mount type=bind.source=/src/webapp,target=/opt/webapp session-web:latest
Copy the code
Load the/SRC /webapp directory of the host to the /opt/webapp directory of the container. This feature is handy for testing, such as placing programs in a local directory to see if the container is working properly.
The path of the local directory must be an absolute path
Previously, using the -v argument Docker automatically created a folder for you if the local directory did not exist.
Now, Docker will report an error if the local directory does not exist when using the –mount argument. The default permission for the Docker host directory to mount is read and write. Users can also specify read-only by adding readonly.
Mount a local host file as a data volume
The mount tag can also mount individual files from the host to the container
Method a #
docker run --rm -it \
--mount type=bind.source=#HOME/.bash_history,target=/root/.bash_history \ Ubuntu: 17.10 bashMethod # 2
docker run --rm -it \
-v $HOME/.bash_history: / root /. Bash_history \ ubuntu: 17.10 bashCopy the code