Docker

Docker Volumes vs. Bind Mounts

The Docker environment offers isolation between the user’s applications and their host machine and containerized applications. Docker containers have a writable layer on the top that enables developers to change the data of the container during its executions. Additionally, the Docker containers lifecycle provides instructions on how these changes are made.

This blog will talk about:

Docker Volumes vs. Bind Mounts

The Docker volumes are the preferred external file system that is utilized to preserve the data produced by containers. On the other hand, bind mounts are host machine file systems that are mounted on Docker containers.

For better understanding, check out the following table that contains the clear difference between volumes and bind mounts:

Volumes Bind Mounts
Easily recoverable through backups. It is difficult to back up and recover.
While mounting it, it required a volume name rather than paths. While mounting, it is essential to specify the path to a host machine.
Volumes are created while creating containers. A mount folder is created when the host machine does not contain the folder.
APIs and CLIs are used for communicating with Docker volumes. Developers cannot access bind mounts using the CLI commands.
Volume stored in /var/lib/docker/volumes. Bind mount can be stored anywhere on a host machine.

How to Create and Inspect Docker Volume?

To generate a Docker volume, run the following command:

docker volume create test_vol1

As you can see, the new volume has been created successfully:

Then, use the provided command to list all the existing volumes:

docker volume ls

Next, inspect the newly created volume for displaying the detailed information using the docker volume inspect command:

docker volume inspect test_vol1

How to Mount Volume to Container in Docker?

To mount the volume to the Docker container, use the following command:

docker run -d --name test_con -p 5000:5000 -v test_vol1:/usr/share/nginx/html nginx

Here:

  • -d option is used for running the container in the background.
  • –name option for adding the container name.
  • test_con is the container name.
  • -p denotes the port number.
  • 5000:5000 is the port number.
  • -v represents the volume.
  • test_vol1 is the volume along with the path:

Another way to mount a volume to the container in Docker is the below-stated command:

docker run -d --name test2_con --mount source=test_vol1,target=/usr/shaet=/usr/share/nginx/html nginx

In order to get the detailed information about the volume, execute the following command:

docker container inspect test2_con

According to the below screenshot, volume has been mounted successfully:

How to Delete Volume in Docker?

If you are required to remove the volume in Docker, run the provided command:

docker volume rm test_vol1

Now, run the below command to list the volume and check whether the deleted volume has been removed or not:

docker volume ls

As you can see, the volume has been deleted successfully from the list:

That’s all! We have provided the difference between volume and bind mounts.

Conclusion

Docker volumes are the preferred external file system that is utilized to preserve the data generated by the Docker containers. However, Bind mounts are host machine file systems that are mounted on the containers. This guide provided the difference between volume and bind mounts.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.