Home > Software engineering >  mount bind inside container is not reflecting outside container
mount bind inside container is not reflecting outside container

Time:06-24

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

[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

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

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

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.

CodePudding user response:

Your bind mount made with mount --bind /container_dir /hostdir will override volume mount inside the container. You simply bind-mount /container_dir over it (same as when you mount e.g. /boot partition over /boot directory on your root filesystem); so anything you write to /hostdir from that point will go only to /container_dir inside container.

What you probably want to do is to swap mount --bind arguments or just make /container_dir a symlink to /hostdir. Or make /container_dir a Docker volume directly and save the hassle.

CodePudding user response:

I am able to achieve above using bind mount type and 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
  • Related