Docker data mount

Docker Data Management

In container management data There are two main ways:

  • Data volume (Volumes)
  • Mount Host Directory (Bind mounts)

img

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 using Linux file or directory was mount, the mount point is specified as 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 or -mount, we recommended -mount, because -mount more concise and easy to understand.

Creating a data volume:

docker volume create my-volume

Displays the information of the data volume:

docker volume inspect my-volume

Start a data volume container loading:
When using docker run command, using the data volume -mount to mount the container, in a docker run can be mounted in a plurality of data volumes.

docker run --name test-web -d -p 8080:8080 \
-v my-volume:/webapp \
test-web:latest

# 或者
docker run --name test-web -d -p 8080:8080 \
--mount source=my-volume,target=/webapp \
test-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 docker rm -v This command is useful when a container is removed.
No primary data volumes may occupy a lot of space, you can be cleaned using the following command:

docker volume prune

Mount Hosting Directory

-Mount directory using a local host can be specified to be mounted to the container:

docker run --name test-web -d -p 8080:8080 \
--mount type=bind,source=/src/webapp,target=/opt/webapp \
test-web

The above command loads the host / src / webapp directory to the container / opt / webapp directory, this function is very convenient when testing, such as the user can place a number of programs to a local directory, to see whether the container is working properly.

Local directory path must be an absolute path.
When using the -v parameter if local directory does not exist, Docker automatically created
when using -mount argument, if local directory does not exist, Docker being given
default permissions Docker host directory is mounted read-write, the user can also specify by increasing readonly Read-only

 

Guess you like

Origin www.cnblogs.com/coding-diary/p/12018589.html