This blog will talk about:
- Docker volumes vs. Bind Mounts
- How to Create and Inspect Docker Volume?
- How to Mount Volume to Container in Docker?
- How to Remove Docker Volume?
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:
As you can see, the new volume has been created successfully:
Then, use the provided command to list all the existing volumes:
Next, inspect the newly created volume for displaying the detailed information using the docker volume inspect command:
How to Mount Volume to Container in Docker?
To mount the volume to the Docker container, use the following command:
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:
In order to get the detailed information about the volume, execute the following command:
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:
Now, run the below command to list the volume and check whether the deleted volume has been removed or not:
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.