Docker Data Management
In container management data There are two main ways:
- Data volume (Volumes)
- Mount Host Directory (Bind mounts)
Data volumes
Data volume is a container for one or more special directory used, it bypasses the UFS, can provide some useful features:
- Data volume can be shared and reused between the vessel
- The data volume changes will take effect immediately
- Updates to the data volume will not affect the image
- The default data volume will always exist, even if the container is deleted
Note: Using data volume, similar to a directory or file mount under Linux, is designated as the mount point of the files in the directory will be hidden away in the mirror, to look at the display is mounted data volumes.
Docker provides two ways to mount, -v, and -mount
Docker new parameter user should select --mount
Experienced users of Docker -v or --volume already very familiar with, but it is recommended to use -mount argument.
Creating a data volume
docker volume create my-volume
View information about the specified data volumes
docker volume inspect my-volume
A starting container to mount the data volume:
When using docker run command, using the tags to the data volume --mount mount container.
Create a container called session-web, and the data volume to a load / webapp directory container.
# 方法一
docker run --name session-web -d -p 8888:8080 --mount source=my-volume,target=/webapp session-web:latest
# 方法二
docker run --name session-web -d -p 8888:8080 -v my-volume:/webapp session-web:latest
Delete data volumes
docker volume rm my-volume
Data volumes are designed to persistent data, its life cycle is independent of the container, Docker does not automatically delete the data volume after the container is removed, and the data garbage collection mechanism to deal with such a container without any reference does not exist volume.
If you need to remove the data volume at the same time a container is removed. You can use this command docker rm -v when the container is removed.
No primary data volumes may occupy a lot of space, use the following command to clean up
docker volume prune
Mount Hosting Directory
Use --mount directory tag may specify a local host to mount the container to
# 方法一
docker run --name session-web -d -p 8888:8080 \
-v my-volume:/webapp \
session-web:latest
# 方法二
docker run --name session-web -d -p 8888:8080 \
--mount type=bind,source=/src/webapp,target=/opt/webapp session-web:latest
The above command loads the host / src / webapp directory to the container / opt / webapp directory. This feature is very convenient when carrying out tests, such as the user can place some programs to a local directory, to see whether the container is working properly.
Local directory path must be an absolute path
Previously, when using the -v parameter Docker if local directory does not exist automatically creates a folder for you.
Now, when using --mount parameters if local directory does not exist, Docker error. The default directory permissions Docker mount host is to read and write, the user can also specify read-only by increasing readonly.
Mount a local host file as a data volume
--mount marker can mount a single file from the host to the vessel
# 方法一
docker run --rm -it \
--mount type=bind,source=#HOME/.bash_history,target=/root/.bash_history \
ubuntu:17.10 bash
# 方法二
docker run --rm -it \
-v $HOME/.bash_history:/root/.bash_history \
ubuntu:17.10 bash