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.
2
Answers
I am able to achieve above using bind mount type and bind-propagation=shared
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.