Summary of mount points involved in docker

1. Scene description

Before introducing the VOLUME command, let's 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 + writable layer of the image. The data persistence of the process operation 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). Can the persistent data of the container process be saved on
the host? 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 on the local host, but the running test environment is placed on the docker container.
In this case, after I modify a file (such as html, js, etc.) on the host, I need to manually enter the container to find the corresponding directory and copy the file to the container. This is obviously more troublesome.

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

For these problems, we can of course think of various solutions. And docker itself provides a mechanism to convert 主机上的某个目录与容器的某个目录(称为挂载点、或者叫卷)关联起来, 容器上的挂载点下的内容就是主机的这个目录下的内容,这类似linux系统下mount的机制. In this way, 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.
In fact, the data is still one copy, which is stored on the host in the directory associated with the mount point in the container. It is not a copy of the data in the container, and there is another copy of data on the host. Therefore, after the container is deleted, the data in the directory corresponding to the mount point on the host is still there.
注意: The mount point on the container is not an ordinary directory. If the directory associated with it on the host is deleted, the mount point on the container is still there, but the content under it
is gone . At this time, if you touch the file name under the mount point of the container, you will be prompted to touch : cannot touch 'filename': No such file or directory. It means that when our
host directory is manually deleted by mistake, the mount point will not work. It also shows that the data under the mount point is actually stored in the directory associated with the host. Instead of
saving a copy on the container, and then syncing it to the directory associated with the host.
注意: The above-mentioned mistaken deletion of the host directory causes the mount point associated with the container to fail to work. It is necessary to create a directory on the host with the name of the project before accidental deletion, and then restart the container
, so that the mount point will take effect and continue to work normally.

2. The location of the container information on the host

insert image description here
The image used in the test below:
insert image description here
Let's introduce the specific mechanism:

3. Through the docker run command

1. Run the command: docker run -d -p 8086:8096 -v /home/mydata:/data 79bcb7a73dd2 /bin/bash
where the -v tag sets a mount point /data in the container (that is, under the root of the container A directory), and associate the contents of the /home/mydata directory under the root on the host to /data.
In this way, the operations on the /data directory in the container or the operations on /home/mydata on the host are completely synchronized in real time, because these two directories actually point to the host directory.
Adding /bin/bash at the end will create and run the container and automatically enter the container.

2. Run the command: docker run -d -p 8087:8096 -v /data 79bcb7a73dd2 /bin/bash The
-v mark above only sets the mount point of the container (/data), and does not specify the associated host directory. At this time, docker will automatically bind a directory on the host (under var/lib/docker/volumes).

The effects of the above two methods can be viewed through the docker inspect command.

First check the information of the running container through docker ps. insert image description here
docker inspect 24388bf5790e
has a lot of content. The intercepted part:
insert image description here
docker inspect 82b44b609da3
insert image description here
Each piece of information under Mounts above records the information of a mount point on the container, and the value of "Destination" is the mount point of the container , the "Source" value is the corresponding host directory.
It can be seen that the host directory corresponding to the second method is automatically created, and its purpose is not to allow modification on the host, but to allow multiple containers to share.

4. 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 a single created container.
The mount point can be created in the image through the VOLUME instruction of the Dockerfile, so that as long as the container created through the image has a mount point.
Another difference is that 通过 VOLUME 指令创建的挂载点,无法指定主机上对应的目录,是自动生成的. Equivalent to the second method above,
only the mount point of the container is set through the docker run -v -v mark, and the associated host directory is not specified.

FROM openjdk:8u212-jre
MAINTAINER ssccxx
#VOLUME /data1 #设置单个挂载点
VOLUME ["/data1","/data2"] #通过数组的方式 设置多个挂载点,每个挂载点会分别对应一个主机上自动生成的目录
ADD platform-customer-post.jar myapp.jar
RUN bash -c 'touch /myapp1.jar'
ENTRYPOINT ["java","-jar","/myapp.jar"]
EXPOSE 8096

Build the image according to the Dockerfile above:
insert image description here
view the built image
insert image description here
Create a container instance through the above image:
docker run -d -it image id
docker inspect container id
insert image description here
You can see the information of the two mount points. Corresponding to the two directories automatically generated on the host.

5. Container shared volume (mount point)

docker run -d --name test1 image id

上面命令中的 镜像id是用前面的Dockerfile文件构建的镜像。 这样容器test1就有了 /data1 和 /data2两个挂载点。
下面我们创建另一个容器可以和test1共享 /data1 和 /data2卷 ,这是在 docker run中使用 --volumes-from标记,如:
可以是来源不同镜像,如:
docker run -d --name test2 --volumes-from test1  79bcb7a73dd2   #这个79bcb7a73dd2镜像中里有一个/tmp挂载点
也可以是同一镜像,如:
docker run -d --name test3 --volumes-from test1  c9aec8050444
上面的三个容器 test1 , test2 , test3 均有 /data1 和 /data2 两个目录,且目录中内容是共享的,任何一个容器修改了内容,别的容器都能获取到。
test2中 还有一个/tmp挂载点

insert image description here
insert image description here
insert image description here

insert image description here
It can be found that the directories on the host corresponding to the /data1 and /data2 mount points in the test1, test2, and test3 containers are all the same directory. That is, the sharing of data between containers is realized.
说明:Although the test3 container is created, even without --volumes-from test1, the /data1, /data2 mount points will be generated. It’s just that the host directory corresponding to the mount generated in this way is not the same as the host directory corresponding to the /data1, /data2 mount points in test1. Data sharing cannot be achieved. After adding --volumes-from test1, the mount point in the image used by test3 will compare whether there is a name that is the same as that in test1. If there is, use the corresponding host directory of test1, and create a new host directory if not.
注意:If --volumes-from is followed by the previous container generated by docker run -v host directory: mount point. Then also share the mount point of this container.
As follows:
insert image description here
Here it should be said that the test4 container has these mount points, and cannot be inherited as mentioned in the screenshot.

6. Best Practice: Data Containers

If multiple containers need to share data (such as persistent databases, configuration files, or data files, etc.), consider creating a specific data container with one or more volumes.
Other containers share volumes from this data container via --volumes-from.

Since the container's volume essentially corresponds to a directory on the host, this data container also does not need to be started.

Such as: docker create --name dbdata myimage echo “data container” #Create a container instance but do not start it

Guess you like

Origin blog.csdn.net/adminstate/article/details/132008741