# Docker Volumes & Bind Mounts

### Docker Volumes

In Docker, a **volume** is a mechanism for persisting data generated and used by containers. Volumes are stored on the host filesystem outside the container's file system, making them an ideal solution for managing persistent data in a containerized environment.

### Creating and Configuring Docker Volumes

**<mark>Step 1 : Create a Volume</mark>**

```plaintext
docker volume create my-vol
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723647200854/e6d25a36-2390-4b83-aed2-0fc06aeac2b9.png align="center")

**<mark>Step 2 : Verify the Volume Creation</mark>**

```plaintext
docker volume ls
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723647246052/f5307e79-0805-46bb-83a6-ecdd91bf86ba.png align="center")

Inspect the created volume

```plaintext
docker inspect my-vol
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723647403971/f9f5611c-d7c3-4301-8f03-9c7c42b3a349.png align="center")

**<mark>Step 3 : Access the Volume</mark>**

Navigate to the volume's mount point on the host system:

```plaintext
cd /var/lib/docker/volumes/my-vol/_data
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723647519926/79436d0d-e252-4780-9294-e1188719bf53.png align="center")

**<mark>Step 4 : Run Containers Using the Volume</mark>**

Launch a container with the volume mounted to port 5050:

```plaintext
 docker run -itd --mount source=my-vol,destination=/usr/local/apache2/htdocs -p "5050:80" httpd
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723647920690/2246fb53-8210-49b4-bfd2-7d5526329536.png align="center")

Launch another container using the same volume but on port 6060:

```plaintext
docker run -itd --mount source=my-vol,destination=/usr/local/apache2/htdocs -p "6060:80" httpd
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648141382/0f1ad6fb-ea70-4840-a4e3-d338baca62d5.png align="center")

**<mark>Step 5 : Verify Content Synchronization</mark>**

Now i have launched 2 containers with different port

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723649565292/1397181d-b394-4bd4-ae5d-8622d33aafb5.png align="center")

Read the index.html file in inside volume

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648423499/1151b8d9-5e43-4581-9d40-9a9f96dc60f9.png align="center")

Access the container on port 5050 and 6060. You'll see the same content because both containers share the same volume.

`5050 port`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648316844/7fa910fc-26d7-40e6-867d-7255181596b8.png align="center")

`6060 port`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648338620/8a5e7bad-d720-42ea-a35f-8b8ed6fad967.png align="center")

Update the `index.html` file in the volume.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648501232/40dc984d-03b3-4063-b92e-4fff38ddbb95.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648590087/cb064757-ec9c-4dca-8cde-0207e6775d29.png align="center")

The changes will reflect immediately in both containers at ports 5050 and 6060.

`5050 port`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648633444/74e0c874-26c7-4639-a7ca-e7f8364bd617.png align="center")

`6060 port`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723648654174/13485315-11a6-4263-ac45-bb12c7b8bd2e.png align="center")

### Docker Bind Mounts

Bind mounts allow you to link a directory or file from the host system to a container. Changes made to the bind-mounted file or directory are immediately reflected on both the host and the container.

### Configuring Bind Mounts :

**<mark>Step 1 : Prepare the Host Directory</mark>**

Create a directory on your host that you want to mount into the container:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723632236975/aed7d268-86b8-448d-8468-6903a4f6a021.png align="center")

**<mark>Step 2 : Run a Container with a Bind Mount</mark>**

Launch a Docker container with the bind mount specified:

```plaintext
 docker run -itd -p "6060:80" -v "/root/docker-sync:/usr/local/apache2/htdocs" httpd
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723632466784/b9047e39-f2db-4279-bf58-bbf1cc2df234.png align="center")

**<mark>Step 3 : Verify the Container</mark>**

Check running containers:

```plaintext
docker ps
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723632544726/7456cbef-cab9-43f6-8608-fca3c61927ff.png align="center")

Access the container’s terminal:

```plaintext
docker exec -it 13ad964d0800 /bin/bash
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723632658657/e8193120-97e5-4c19-8f44-3363d23f057b.png align="center")

Navigate to the `/usr/local/apache2/htdocs` directory inside the container:

Initially, the directory may be empty.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723632815125/e0455e27-3e77-4e1b-8bbd-13e1f3491515.png align="center")

Exit the container

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723632977201/789e68ee-5b2e-4750-9d3d-3e799f7bb1d8.png align="center")

Now, Check the container port number, it is showing default content

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723633059764/34c1f517-e1a4-404a-93e7-198b700984de.png align="center")

Create an `index.html` file in the `/root/docker-sync` directory on your host machine.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723633178097/f8289fd0-6216-4ec5-a3c3-a15b1d72a819.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723633222815/058ce46e-b0e6-4623-b2ad-161e6d9e7e27.png align="center")

**<mark>Step 4 : Check Content Synchronization</mark>**

Refresh the container’s content by accessing it via the public IP and port 6060. The `index.html` file content should automatically update inside the container reflecting changes made on the host.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723633282078/541bf956-274f-40a2-b2c0-747b3a57fe35.png align="center")

### Conclusion

**Docker Volumes**: Docker volumes are ideal for persisting and sharing data across containers, ensuring data remains consistent and managed even when containers are recreated.

**Docker Bind Mounts**: Bind mounts enable real-time synchronization between host files and containers, making them perfect for development scenarios where immediate file updates are needed.
