In this demo, we will test the Bind Mounts.
Let's create a directory called bind-data on our Docker
host's home directory. Now run a container called
bind-ubuntu from ubuntu:latest image and bind its
tmp or temp directory to the newly created bind-data directory
using bind-mount. As usual, let's
see if the container is running. Yes it is.
Now it is time to inspect the bind-mount information,
and we have Mount Type, which is bind mount along with
Source and Destination paths, which are just as we
had provided them. Furthermore, we have read/write
permission set up to true, which means changes in
the files will reflect on both the sides.
It is probably the least secure way to mount your
container data to persistent storage, but for now,
it works. Lastly, we have bind Propagation,
it is an interesting aspect. Bind Propagation is a
policy which determines the bilateral access to
the directories created within the mount point
source and destinations. In other words, it will
decide whether subdirectories of Mount will be
associated with a mount or not, 'rprivate' is the
default value, which means that any subdirectory
within source or destination of Mount will not
reflect on either side. Let's execute bind-ubuntu
container with bash command and create a file
called foo.txt. We are creating it within the
container's tmp directory which is the mount destination.
Once we are done let's exit the container.
Now let's access the source of mount
point which is within home directory of Docker host.
We can see the bind-data directory
reflecting here, let's open it. And there we go,
foo.txt is present. Now let's try making
changes the other way around. We have seen
destinations update reflecting on source.
Now let's update source to see if destination reflects
the changes as well. Mind well that our container
is shut at the moment and we are creating a new
file called hello.txt.
Let's go back to terminal and execute the container again so that
we can navigate to its tmp or temp directory, hit ls
to see the list of files, and there we go. We had
stopped the container with one file, but now it has two of them
or bind-mount is working successfully.