Docker storage volumes

Why do you need storage volumes

Docker image formed by a plurality of read-only overlay of, when starting container, Docker loads read-only image layer and add a layer on top of the mirror stacks reader.
If you are running in the container modify the existing file that already exists, then the file will be copied from read-only to read and write layer below layer to layer read-write, read-only version of the file is still there but has been read layer copy of the file is hidden, this is the copy (COW) write mechanism.

Data storage problems

Docker containers stored data, the following problems:

  • United stored in the file system, easy access to the host
  • Data sharing between containers inconvenience
  • Delete their data will be lost container

Close and restart the container, the container data is not affected. However, the container will be deleted before the changes are lost.

Solution: Volume (volume), or to facilitate storage volume is called distinguished.
Volume is one or more directory on the container, such joint may bypass the file system directory, and a directory on the host binding (association).

Data volumes

Volume is created when the container is initialized, the data volume provided by the base image in the copy will be completed this period.

Volumn mind is independent of the vessel's life-cycle data persistence, so when you remove containers will not delete the volume, nor will it even if volumes do not referenced garbage collection operation.

Volumes docker provides a separate container data management mechanisms:

  • It can be thought of as a static image file, such as a program. Volume analogy to dynamic content, such as data. Thus, the mirror can be reused, and volumes can share
  • Realization program (image) data and the (volume) separating and isolating the host program (mirror) and the mirrored. No need to consider the environment where the host vessel mirror that mirrored the user.

Volumes used in the container

Now, when you create a container, with volumes of related options.

Volumes types

There are two types of roll Docker, each type there is a mount point in the container, but the position on a host of different:

  • Bind-mount volume: a directory and a host on the container need to specify the
  • Docker-managed volume: need to specify the directory in the vessel, is maintained by the directory on the host docker

As a docker run command using the -v option, you can use the Volume.

Docker-managed volume

Open a session to create a container and bound volume:

$ docker run --name v1 --rm -it -v /data busybox

Operating on the host directory
inquiry inspect the container in another session, but more content. Volumes can be seen in the volume of information:

            "Volumes": {
                "/data": {}
            },

There are information points of the mount, where the use of the -f parameter look at the contents of Mounts:

$ docker inspect -f '{{json .Mounts}}' v1
[{"Type":"volume","Name":"bdd48fb729e802b7d3a067da74b748037e83ff770a84f7215c657f6cc2af2c9d","Source":"/var/lib/docker/volumes/bdd48fb729e802b7d3a067da74b748037e83ff770a84f7215c657f6cc2af2c9d/_data","Destination":"/data","Driver":"local","Mode":"","RW":true,"Propagation":""}]
$ 

The host automatically assigned path volume in the Source field, the following method of obtaining a direct call path and ls command:

$ docker inspect -f '{{range .Mounts}}{{.Source}} {{end}}' v1 | xargs ls
$ docker inspect -f '{{with index .Mounts 0}}{{.Source}}{{end}}' v1 | xargs ls

Mounts in an array, there may be multiple mount points. The first command line is traversing all the directories, commands the output of the second line is only a first directory.
Directory mount point, the container and the host can share data. Container for a directory, in the host machine can view. On the contrary, in the host machine to modify the directory, it is possible to view the container.

Remove the mirror
above command to create a container used --rm parameters, so that once the container is stopped, the container will also be deleted automatically. Remove the vessel, Docker-managed volume will also delete.
So this is a temporary volume, the data stored within the volume will still be lost as a container is removed. Does not solve the object of the data container persisted, but now the data container can be shared and host, and the data stored in the I / O is directly determined by the host file system. Additional benefit is that users do not have to manage this volume, volume data temporary memory is deleted with the release of a container is removed. However, suitable for use in the outer container needs to be shared temporary data storage.

Bind-mount Volme

Or the parameters used before, but the content -v parameters are provided in two parts, a directory and a volume directory of the host, separated by a colon: -v HOSTDIR:VOLUMEDIR.

Binding Bind-mount Volme
open a session to create a container and bind Bind-mount Volme:

$ docker run --name v2 --rm -it -v ~/data/volumes/v2:/data busybox

You can still use docker inspect command to see the details, here is not to demonstrate.
Path specified here, if the host does not exist, docker will be created automatically.

Delete container
after container delete files on the host will remain. This realization of the data can still be used after the container removed.

Shared Volume

Said earlier, docker There are two types of volume two it has been said. A certain kind of a shared volume here is essentially the above two types, it is realized only between containers share the same volume of the two methods.

More container volume uses the same host directory
that is no problem, the same directory on the host is allowed to use multiple containers.

Volume copy using other containers
using --volumes-from options:

$ docker run --name v3.0 --rm -itd -v /data busybox
e24397fffb44717a7f140010c829ec88f540ed7d95f7c8fb328a35a819becfb0
$ docker run --name v3.1 --rm -itd --volumes-from v3.0 busybox
28543eb6fc13e972a000ab955b9623630ebb9d90c8d3dc37a8b7608f24188926
$ docker container attach v3.0
/ # touch /data/test_v3_0
/ # exit
$ docker container attach v3.1
/ # ls /data
test_v3_0
/ # exit
$ 

Here is an example of replication Docker-managed volume, for Bind-mount Volme is the same.

Sharing network
network can also share that alliance network. Here, the volume can be shared. In some scenarios, there are two or more containers may need to share the same network and share the same volume.

Guess you like

Origin blog.51cto.com/steed/2422611