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.

[email protected]:~$ mkdir bind
[email protected]:~$ cd bind/
[email protected]:~/bind$ vi index.html

Where contents of index.html file are as follows-

[email protected]:~/bind$ cat index.html
<html>
        <head>
                <title>test</title>
        </head>
        <body>
                <h1 align="center">Docker BindMount Point Test</h1>
        </body>
</html>
[email protected]:~/bind$ this is a test line here


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. 

[email protected]:~/bind$ cd ..
[email protected]:~/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-

[email protected]:/# cd /tmp/
[email protected]:/tmp# ls
test
[email protected]:/tmp# cd test/
[email protected]:/tmp/test# ls
index.html
[email protected]:/tmp/test# cat index.html
<html>
        <head>
                <title>test</title>
        </head>
        <body>
                <h1 align="center">Docker BindMount Point Test</h1>
        </body>
</html>
[email protected]:/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-

[email protected]:~/bind$ docker container run -itd -v bind:/tmp/test/ ubuntu
63f5abfbd92aceb91e60232508cd5c1543d52e0975cd8afb66b777ac25b08770
[email protected]:~/bind$ docker volume ls
DRIVER    VOLUME NAME
local     bind
[email protected]:~/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-

[email protected]:~/bind$ docker container run -it -v $(pwd):/tmp/test/ ubuntu
[email protected]:/# cd /tmp/test/
[email protected]:/tmp/test# ls
index.html
[email protected]:/tmp/test# cat index.html
<html>
        <head>
                <title>test</title>
        </head>
        <body>
                <h1 align="center">Docker BindMount Point Test</h1>
        </body>
</html>
[email protected]:/tmp/test#



The above command can also be written in below manner -

[email protected]:~/bind$ docker container run -it --mount type=bind,source=$(pwd),target=/tmp/test ubuntu bash
[email protected]:/# cd /tmp/test/
[email protected]:/tmp/test# ls
index.html
[email protected]:/tmp/test#