Docker

What is Docker Bind Mounts?

While working on a development project in Docker, developers usually want to make modifications in the code and see the changes reflected immediately without rebuilding the container. In this situation, use a bind mount to mount the code directory on their local host machine into the container. Upon doing so, the modifications made on the host are immediately reflected inside the container. Moreover, it is useful when the container is deleted or rejected as the data is not lost.

This article will illustrate:

What is Docker Bind Mounts?

A Docker bind mount permits users to map a particular file/directory on the host machine to a file/directory within a container. This way, users can share data between the host and the container, and also persist the data even after the container is stopped or deleted. Any changes made to files in the shared directory or file are visible from both the container and the host machine.

How Bind Mount Work in Docker?

Let us take an example and see how bind-mount works in Docker. Suppose that we have an empty directory “Test” located at “C:\Docker” on a local system. Now, we want to access the “Test” directory’s content at the location “/app” from within a particular container. In this example, run a container from the official Docker image i.e., “nginx” and utilize the bind mount to mount a specific directory from the host machine into the container.

For a better understanding, follow the provided steps.

Step 1: Bind Mount Directory From Host Machine to Container

First, utilize the “docker run -d –name <container-name> -v <source-path>:<destination-path> <image-name>” command and run a container. It binds the mount directory from the host machine to the container:

docker run -d --name myCont -v C:/Docker/Test:/app nginx:latest

 
Here:

    • -d” option is used to execute the container in the background.
    • –name” is used to define the container name.
    • myCont” is our container name.
    • -v” option creates a volume in the container that maps the source directory on the host machine to the target directory in the container.
    • C:/Docker/Test” is the path of the source directory (local machine).
    • /app” is the target directory (container) path.
    • nginx:latest” is the latest Docker image:

 

This command has created a container and allowed it to access files from the host machine and making it easier to manage data persistence.

Step 2: Create a File in Source Directory on Host Machine

Then, navigate to the source directory path i.e., “C:/Docker/Test” on the host machine and create a plain text file in it. For instance, we have created a “Demo” file:


Step 3: Access the Host Machine File Inside the Container

Now, type out the below-provided command to access the container’s content and run commands inside it:

docker exec -it myCont bash

 

Upon doing so, the container shell will open.

After that, list the container’s content using the provided command:

ls

 

In the above screenshot, all the content of the container can be seen. Choose the desired directory and navigate to it.

Redirect to the “app” directory:

cd app

 
Then, list the “app” directory content to verify the local machine file is available in it:

ls

 

It can be observed that the “Demo.txt” file is available inside the container, and we can access it.

Step 4: Create File Inside Container

Next, create another file inside a container using the “touch” command:

touch new.txt

 

We have created a “new.txt” file.

Then, verify the newly created file using the below-listed command:

ls

 

It can be seen that the file “new.txt” has been created successfully inside the container.

Step 5: Verify File on the Local System

Finally, navigate to the local machine path and check if the “new.txt” file is available or not:


As you can see, the “new.txt” file is available on the local machine, and we can access it. This indicates that the modifications are reflected on the local machine also.

Step 6: Remove Docker Container

Now, remove the container via the “docker rm” command along with the container name:

docker rm myCont

 

The “myCont” container has been deleted successfully.

Step 7: Ensure Data Persisted on the Local Machine

After deleting the container, verify whether the data persisted on the local machine on not:


As you can see, the changes persisted even after deleting the bind-mount container.

Conclusion

Docker bind mount is used to map a directory or file from the host system into the container. It creates a link between the specified directory or file on the host and the container’s filesystem. It makes it easy and simple to deal or work with files that are stored outside the container. Any changes made to files in the shared directory or file will be reflected in both the host and container. This article has explained about Docker bind mount and its working in Docker.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.