Open In App

How To Use Bind Mount In Docker?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Bind mounts and the host filesystem are tightly related. The host filesystem is immediately impacted by changes made to a bind mount, and vice versa. When you use a bind mount to remove a container, the related data stays on the host.

What is Docker Volume?

Applications can be run independently using Docker containers. By default, when a container is deleted or recreated, all of the modifications (data) within it are lost. Docker volumes and bind mounts can be useful if we wish to save data in between runs. In order to protect data created by the running container, Docker volumes are file systems mounted on Docker containers.

What Is Bind Mount In Docker?

Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can be modified at any time. You can create a file on the host system and attach it to the container where you want to maintain the state of the Docker. You can also use the bind mount for stateful applications.

What is the Docker Volume Driver Plugin?

It’s a piece of code or software that is responsible for creating storage and attaching the storage to the container.

Types Of Docker Volume Driver Plugins are Available

In docker we mainly use two docker driver volume plugins, They are.

1. Local Driver (Steps To Create Custom Docker Volume Using Local Driver)

Follow the steps below to use the local driver to create the volume using a local driver.

Step 1: Use the below command to create the docker volume using a local driver.

Docker Volume Create

docker create volume -d local <Name Of the Volume>

“LOCAL” name of the plugin and give the name of volume that you want. In the above image you can see the volume with my coustm name GFG-Volume was Created.

2. REX-RAY Driver

To learn more about the docker rex-ray plugin please refer to the the following article.

What Is Docker REX – RAY Plugin ?

How To List The Docker Volumes?

You can list all the docker volumes that are available in the docker by using a simple command as shown below.

docker volume ls

“docker volume ls” will list all the docker volumes which are avalible in the docker host and also it will list the volume which are managed out side of the docker host as shown in the image below.

docker volume ls

Steps To Create And Attach Bind Mount To The Docker Container

Follow the steps Mentioned below to create and attach the bind mount to docker container.

Step 1: First create an empty directroy in the docker host which you want to attach the application that you are dockerizing the application.

Mkdir <Name Of the Volume You wan to Create>

Step 2: Know this directory will act as an volume on the docker host you need to mount this to the path where the data will br stored in the application as show in the image below you can use the following command to do this.

 sudo docker run -d <Container ID> --name <Name Of the Container> -v <Host Path>:<Container Path>

All the steps

/home/ubuntu/nginx is the host path where my directory of volume is located and the /app is the contaienr path.

Where are Docker bind mounts stored?

Docker bind mounts are kept directly on the filesystem of the host computer. A bind mount allows you to link a particular file or directory on the host to a directory inside the container. Any modifications made within the container are also reflected in the host’s connected location.

Docker Bind Mount – FAQ’s

What are two differences between a docker volume and a bind mount?

  • Docker Volumes: Stored outside of the container filesystem, Docker volumes are controlled by Docker. Docker handles managing and maintaining these volumes; they have a specific placement on the host machine. Volumes are perfect for distributing data across containers and for storing data between container restarts.
  • Bind Mounts: In contrast, bind mounts establish a direct connection between a particular file or directory on the host computer and a directory within the container. This indicates that Docker does not directly manage the data; instead, it is stored on the host disc. Although they may need more manual maintenance, bind mounts offer a simple mechanism for the host and the container to communicate files and directories.

Can I mount a file in Docker?

Yes, you can mount a file into a Docker container using both Docker volumes and bind mounts.

docker run -v /path/on/host:/path/in/container -it your_image touch /path/in/container/your_file.txt


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads