Docker Bind mount

Using Docker bind mounts, you can mount a file or folder on the host machine inside the container. The file or directory is referenced by its absolute path on the host machine.

To demonstrate this, let's create a directory bind on our host machine and create a file index.html in it.

gaurav@learning-ocean:~$ mkdir bind
gaurav@learning-ocean:~$ cd bind/
gaurav@learning-ocean:~/bind$ vi index.html

Where contents of index.html file are as follows-

gaurav@learning-ocean:~/bind$ cat index.html
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <h1 align="center">Docker BindMount Point Test</h1>
  </body>
</html>

We will see how we can access this same file inside our container.

Create a container using Ubuntu image and binding the directory we just created to the /tmp/test/ directory inside the container.

gaurav@learning-ocean:~/bind$ cd ..
gaurav@learning-ocean:~/bind$ docker container run -it -v /home/gaurav/bind:/tmp/test/ ubuntu

Note: As we have seen in docker volumes if the /tmp/test/ is not present, docker will create it automatically and also, in case the path already exists and if there is some data already then, that data will get hidden but will still be accessible to the user.

Now verifying the output of the above command-

root@cfff4a121263:/# cd /tmp/
root@cfff4a121263:/tmp# ls
test
root@cfff4a121263:/tmp# cd test/
root@cfff4a121263:/tmp/test# ls
index.html
root@cfff4a121263:/tmp/test# cat index.html
<html>
        <head>
                <title>test</title>
        </head>
        <body>
                <h1 align="center">Docker BindMount Point Test</h1>
        </body>
</html>
root@cfff4a121263:/tmp/test#

The container has the test directory and also index.html in place.

If we make any changes inside the container, it will be reflected in the host machine's directory as well.

Make sure that while providing the path of the directory, you mention the entire path and not the relative path as docker will consider it a new volume-

gaurav@learning-ocean:~/bind$ docker container run -itd -v bind:/tmp/test/ ubuntu
63f5abfbd92aceb91e60232508cd5c1543d52e0975cd8afb66b777ac25b08770
gaurav@learning-ocean:~/bind$ docker volume ls
DRIVER    VOLUME NAME
local     bind
gaurav@learning-ocean:~/bind$

Passing the current directory as bind volume

In case you want to bind the present working directory, then pass the $(pwd) to the command as below-

gaurav@learning-ocean:~/bind$ docker container run -it -v $(pwd):/tmp/test/ ubuntu
root@23d4c6b42dd0:/# cd /tmp/test/
root@23d4c6b42dd0:/tmp/test# ls
index.html
root@23d4c6b42dd0:/tmp/test# cat index.html
<html>
        <head>
                <title>test</title>
        </head>
        <body>
                <h1 align="center">Docker BindMount Point Test</h1>
        </body>
</html>
root@23d4c6b42dd0:/tmp/test#

The above command can also be written in below manner -

gaurav@learning-ocean:~/bind$ docker container run -it --mount type=bind,source=$(pwd),target=/tmp/test ubuntu bash
root@221bfae70469:/# cd /tmp/test/
root@221bfae70469:/tmp/test# ls
index.html
root@221bfae70469:/tmp/test#