简体   繁体   English

在容器内安装绑定不反映在容器外

[英]mount bind inside container is not reflecting outside container

I am trying to bind host dir to some dir inside container and expecting changes done inside container should reflect on host.我正在尝试将主机目录绑定到容器内的某个目录,并期望在容器内完成的更改应该反映在主机上。

Here is the steps I have followed这是我遵循的步骤

created /hostdir on host and run ubuntu container in privileged mode在主机上创建 /hostdir 并以特权模式运行 ubuntu 容器

[root@nhbdlin03 ~]# mkdir /hostdir
[root@nhbdlin03 ~]# docker run -itd --privileged --name ubuntu -v /hostdir:/hostdir:z ubuntu
76aebded33274e95a6f569d0831aee4df27e9f200a8fd0401448239bd6f5bf80
[root@nhbdlin03 ~]# docker exec -it ubuntu bash

creating a container_dir inside container在容器内创建一个 container_dir

root@76aebded3327:/# mkdir /container_dir

binding the two directory (successfull)绑定两个目录(成功)

root@76aebded3327:/# mount --bind /container_dir /hostdir

creating a file named hello.txt inside /container_dir在 /container_dir 中创建一个名为 hello.txt 的文件

root@76aebded3327:/# cd container_dir/
root@76aebded3327:/container_dir# touch hello.txt

its get reflected inside /hostdir as it is bind mount to /container_dir它反映在 /hostdir 中,因为它绑定挂载到 /container_dir

root@76aebded3327:/container_dir# ls /hostdir/
hello.txt

exit container and check on host , is the same reflected退出容器并检查主机,反映相同

root@76aebded3327:/container_dir# exit

[root@nhbdlin03 ~]# ls /hostdir/
[root@nhbdlin03 ~]# ls /hostdir/ | wc -l
0
[root@nhbdlin03 ~]#

the content are not getting reflected.内容没有得到反映。
I am missing something or doing completely wrong, please help me in the right direction.我错过了一些东西或做错了,请帮助我朝着正确的方向前进。

Your bind mount made with mount --bind /container_dir /hostdir will override volume mount inside the container.使用mount --bind /container_dir /hostdir进行的绑定挂载将覆盖容器内的卷挂载。 You simply bind-mount /container_dir over it (same as when you mount eg /boot partition over /boot directory on your root filesystem);您只需在其上绑定挂载/container_dir (与在根文件系统上的/boot目录上挂载/boot分区时相同); so anything you write to /hostdir from that point will go only to /container_dir inside container.因此,您从那时起写入/hostdir的任何内容都只会转到/container_dir container_dir。

What you probably want to do is to swap mount --bind arguments or just make /container_dir a symlink to /hostdir .您可能想要做的是交换mount --bind参数或只是使/container_dir成为/hostdir的符号链接。 Or make /container_dir a Docker volume directly and save the hassle.或者直接将/container_dir做成 Docker 卷,省去麻烦。

I am able to achieve above using bind mount type and bind-propagation=shared我可以使用绑定挂载类型和 bind-propagation=shared 来实现上述目标

[root@nhbdlin03 ~]# docker run -itd --name ubuntu --privileged --mount type=bind,source=/hostdir,target=/hostdir,bind-propagation=shared ubuntu
2cb814f600ed261ae5e50fcdaec6b5a5a17f3d41d37be307765e20274d918a25
[root@nhbdlin03 ~]# docker exec -it ubuntu bash
root@2cb814f600ed:/# mkdir /container_dir

root@2cb814f600ed:/# touch /container_dir/hello
root@2cb814f600ed:/#  mount --bind --make-shared /container_dir /hostdir
root@2cb814f600ed:/# ls /hostdir/
hello
root@2cb814f600ed:/# exit
exit
[root@nhbdlin03 ~]# ls /hostdir/
hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM