Docker host and container file sharing -v and VOLUME

Before introducing the VOLUME command, let's take a look at the following scenario requirements:

1) The container is created based on the image. The final container file system includes the read-only layer + the writable layer of the image. The data persistence of the process operations in the container is stored on the writable layer of the container. Once the container is deleted, the data is gone unless we manually back it up (or create a new image based on the container). Is it possible In this way, even if the container is deleted, the data is still there.

2) When we are developing a web application, the development environment is locally on the host, but the running test environment is placed on the docker container.

In this case, after I modify files (such as html, js, etc.) on the host, I need to synchronize them to the container. This is obviously more troublesome.

3) Multiple containers running a set of associated services, what if they want to share some data ?

We can certainly think of various solutions to these problems. Docker itself provides a mechanism to associate a directory on the host with a directory (called a mount point, or volume) on the container. The content under the mount point on the container is the host's The contents of the directory, which is similar to the mount mechanism under the linux system. In this case, when we modify the contents of the directory on the host, we do not need to synchronize the container, and it will take effect immediately for the container. Mount points can be shared by multiple containers.

Below we introduce the specific mechanism.


1. Through the docker run command

1. Run the command: docker run --name test -it  -v /home/xqh/myimage:/data  ubuntu /bin/bash

The -v flag sets a mount point /data in the container (that is, a directory in the container), and associates the contents of the /home/xqh/myimage directory on the host with /data.

In this way, the operations on the /data directory in the container and the operations on /home/xqh/myimage on the host are completely synchronized in real time, because these two directories actually point to the host directory.

2. Run the command: docker run --name test1 -it  -v /data ubuntu  /bin/bash

The -v flag above only sets the mount point of the container, and does not specify the associated host directory. At this time, docker will automatically bind a directory on the host. You can see it with the docker inspect command.

"Mounts": [
            {
                "Name": "2aebfaef1bb42f2486ed236abe897441f64a3e880c64c012fca113022d3aec50",
                "Source": "/local/docker/var/lib/docker/volumes/2aebfaef1bb42f2486ed236abe897441f64a3e880c64c012fca113022d3aec50/_data",
                "Destination": "/local/daasdocker/sofeware",

                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""

            },

Each piece of information under Mounts above records the information of a mount point on the container. The "Destination" value is the mount point of the container, and the "Source" value is the corresponding host directory.

It can be seen that the host directory corresponding to this method is automatically created. The purpose is not to modify it on the host, but to share it with multiple containers.

2. Create a mount point through dockerfile

The mount point created by the -v flag of the docker run command described above is only valid for the created container .

A mount point can be created in the image through the VOLUME command of the dockerfile, so that as long as the container created through the image has a mount point.

Another difference is that the mount point created by the VOLUME command cannot specify the corresponding directory on the host and is automatically generated.

grammar:

VOLUME ["/var/log/"]
VOLUME / var / log
VOLUME / var / log / var / db
VOLUME ["/data1","/data2"]

The above dockfile specifies two mount points /data1 and /data2 through the VOLUME directive.

We use docker inspect to view the container generated by the image created by this dockerfile

You can see the information of two mount points.

3. Container Shared Volume (Mount Point)

docker run --name test1 -it myimage /bin/bash

myimage in the above command is the image built with the previous dockerfile. In this way, the container test1 has two mount points /data1 and /data2.

Next we create another container to share the /data1 and /data2 volumes with test1. This is done using the --volumes-from flag in docker run , such as:

It can be a mirror from different sources, such as:

docker run --name test2 -it --volumes-from test1  ubuntu  /bin/bash

It can also be the same image, such as:

docker run --name test3 -it --volumes-from test1  myimage  /bin/bash

上面的三个容器 test1 , test2 , test3 均有 /data1 和 /data2 两个目录,且目录中内容是共享的,任何一个容器修改了内容,别的容器都能获取到。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325893471&siteId=291194637