# Day 19 - Exploring Docker for DevOps Engineers: Dive into Docker Volume  (Dec 15, 2023)

### Docker Volume

Docker Volume is a powerful feature that allows you to create isolated storage areas accessible by containers. This enables the storage of crucial data, such as databases, outside the container itself, preventing data loss when the container is deleted. Additionally, multiple containers can mount the same volume, ensuring consistent data across applications.

#### Task-1: Multi-Container Docker Compose

Your first task is to create a multi-container Docker Compose file that efficiently brings up and takes down containers simultaneously. This practical exercise involves creating application and database containers. Here are some hints to guide you through:

* Utilize `docker-compose up -d` to start a multi-container application in detached mode.
    
* Explore `docker-compose scale` to adjust the number of replicas for a specific service.
    
* Monitor container status with `docker-compose ps` and view service logs with `docker-compose logs`.
    
* Implement `docker-compose down` to stop and remove all associated containers, networks, and volumes.
    

#### Answer-1:

```plaintext
version: '3'
services:
  app:
    image: your-app-image
  database:
    image: your-database-image
```

**Commands:**

* `docker-compose up -d` - Start containers in detached mode.
    
* `docker-compose scale app=3` - Adjust the number of replicas for the app service.
    
* `docker-compose ps` - View the status of all containers.
    
* `docker-compose logs` - View logs of a specific service.
    
* `docker-compose down` - Stop and remove all associated containers, networks, and volumes.
    

#### Task-2: Docker Volumes and Named Volumes

Your second task involves mastering the usage of Docker Volumes and Named Volumes to share files and directories among multiple containers. Follow these steps:

* Create two or more containers that read and write data to the same volume using `docker run --mount`.
    
* Verify data consistency across all containers using `docker exec` to run commands inside each container.
    
* Explore `docker volume ls` to list all volumes and `docker volume rm` to remove volumes when no longer needed.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702627387712/953bf6e7-7ed1-49f8-9ee0-72c310a7f0be.png align="left")
    

#### Answer-2:

* **Command to create containers with shared volume:**
    
* ```plaintext
          docker run -d --name container1 --mount source=my_volume,target=/data your-image
          docker run -d --name container2 --mount source=my_volume,target=/data your-image
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702632000459/6d2689f0-7206-444b-966a-57f813983740.png align="center")
    
* **Verify data consistency:**
    
* ```plaintext
          docker exec -it container1 /bin/bash
          echo "Data from container1" > /data/test.txt
          exit
          
          docker exec -it container2 cat /data/test.txt
          # Output should be "Data from container1"
    ```
    
* **Commands to list volumes and remove volume:**
    

```bash
docker volume ls
docker volume rm my_volume
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702627621433/1d2859ad-3372-488b-a32b-2fbb760ee49f.png align="center")
