Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of mounting Container data Volume in Docker

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you Docker container data volume mount example analysis, I hope you have something to gain after reading this article, let's discuss it together!

Case 1: Files that do not exist locally are mounted to containers where files exist

First, when the file does not exist locally, but the file exists in the container, try to mount the file that does not exist into the container where the file exists. Take an Alpine mirror as an example. Here, a modified Alpine mirror is labeled with a new label called volume_test:

#No test file exists in local directory.$ docker run --name=test -v ~/test.txt:/etc/hosts -d volume_test0cba2e50229df7508c616bd456c4ab131f2fe1a88385c34f8a5876fbc577b176docker: Error response from daemon: oci runtime error: rootfs_linux.go:53: mounting "/var/lib/docker/devicemapper/mnt/6b83c07ebedcb828f34cac69eac5a85ce3a5f59e1e8688c8dae40198671d0ecb/rootfs/etc/hosts" to rootfs "/var/lib/docker/devicemapper/mnt/6b83c07ebedcb828f34cac69eac5a85ce3a5f59e1e8688c8dae40198671d0ecb/rootfs" caused "not a directory".# Failed to start container.

Case 2: There is no folder locally mounted to the container and there is a folder

Then mount the folder that does not exist locally to the folder that exists in the container. There is a folder/srv in the volume_test image, and there is an index.php file in the folder.

#srv folder does not exist in local directory.$ docker run --name=test -v ~/srv:/srv -d volume_test c71cf1cfa4932e3e398a7d6c4e2ae94f915b832f5506e374aedb19af4cb1ac62#startup normal, but enter container found directory is empty.$ docker exec -it test sh/srv # ls/srv #

As the above two examples have shown us, a volume is mounted by overwriting the local directory into the container. That is, when the host file does not exist, it cannot be mounted; when the folder does not exist, mounting to the container will overwrite the container's original directory with an empty folder.

Case 3: The host computer has a file mounted to the container and no file exists.

Let's continue by assuming that the file exists on the host but not in the container:

#The test.txt file $ docker run --name= test-v ~/test.txt:/srv/test.txt-d volume_test2d6853c10643a735ae3d7f3aaac8c6344f9c75170e531f613d08db7cdf484e54#exists in the local directory. The/srv folder originally contains an index.php.$ docker exec -it test sh/srv # lsindex.php test.txt/srv # #You can see that the file was successfully mounted.

Case 4: There is a folder on the host computer that is mounted to the container. There is no folder.

Next, there is a folder in the host, but the container does not exist in this folder. There is a hello file in the test folder of the host:

$ docker run --name=test -v ~/test:/srv/test -d volume_test c935ffa0d9fc5e5ac8f213a51a878e71056472b0597d2e385a29e5c748012958#Enter the container to check if there is a test folder and if there is a hello file in the folder.$ docker exec -it test sh/srv # lsindex.php test/srv # cd test//srv/test # lshello/srv/test #

The above two examples illustrate that if there is no file inside the container, the host directly mounts it.

Case 5: The host folder is mounted to the container file

Next, suppose there is a test folder on the host and a file named test exists inside the container. What happens when you mount it like this?

$ docker run --name=test -v ~/test:/srv/test-d volume_test 385bc78e5333460da11f04535da27a3fd226df218f95c970ff2dd5609b17f816docker: Error response from daemon: oci runtime error: rootfs_linux.go:53: mounting "/var/lib/docker/devicemapper/mnt/fd5c42e844c3550d1a372ed939ed57f90dcacbd375dfed1bedfbb71ef6f3f185/rootfs/etc/hosts" to rootfs "/var/lib/docker/devicemapper/mnt/fd5c42e844c3550d1a372ed939ed57f90dcacbd375dfed1bedfbb71ef6f3f185/rootfs" caused "not a directory".

The above situation is not unexpected is a startup error.

Case 6: Mounting a folder with the same name

So suppose the host is a folder, and the container is also a folder. The contents of the two folders are different. There is a hello file inside the host, and there is an index.php in the container folder:

$ docker run --name=test -v ~/srv:/srv -d volume_test 3aec30122bd7010c694e0ff8b77b7b6bb6f850c258786db125313060fad43b $ docker exec-it test sh/srv # lshello/srv # #You can see that the host folder overrides the folder inside the container.

Case 7: Mount the file with the same name

Suppose the host has a test.txt file with Hello World in it, and there is also a test.txt file with Hi World in the container. Now mount the file:

$ docker run --name=test -v <$/test. txt: /srv/test. txt-d volume_test 047cbfe45b5bc868c864fe94f7a22643d52b644947f40260097dbb579de56c5c$ docker exec -it test sh/srv # cat /testHello World/srv # #Host overwrites container files.

Case 8: Host files mounted to container folders

In the last case, there is a file test.txt on the host, and there is a test folder inside the container. Now mount the file into the folder:

$ docker run --name=test -v ~/test.txt:/test -d volume_test 59b5fd74a1e9e17aa2a6a9be7900b16c7dd4b3c424a4fa72a7671fa1c51bdf69docker: Error response from daemon: oci runtime error: rootfs_linux.go:53: mounting "/var/lib/docker/devicemapper/mnt/b201054ed36a189b5abb599082d0b5bcbe31d07611a0985deefd79d1221447fd/rootfs/home" to rootfs "/var/lib/docker/devicemapper/mnt/b201054ed36a189b5abb599082d0b5bcbe31d07611a0985deefd79d1221447fd/rootfs" caused "not a directory".# Failed to initiate.

Summary:

File startup parameters in the host file container (bold means does not exist) Container startup does not exist File-v ~/test. tx:/etc/hosts startup error does not exist Folder-v ~/srv:/srv startup normal file does not exist-v~/test.tx:/srv/test.txt Startup normal folder does not exist ~/test: /srv/test Startup error Folder folder-v ~/srv:/srv startup normal file folder-v~/test.tx:/srv/test.txt Startup error After reading this article, I believe you have a certain understanding of "Docker container data volume mounting example analysis", if you want to know more related knowledge, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report