# Day 19 Task: Docker Volume for  DevOps Engineers

## Introduction on docker volumes :

* Docker volumes are a feature in Docker that allows you to manage and persist data between containers.
    
* They provide a way to store and share data separate from the container itself. Volumes are used to:
    
    1. **Persist Data:** Volumes ensure that data generated or used by containers is retained even if the container is stopped or removed.
        
    2. **Share Data Between Containers:** Volumes enable multiple containers to access and share the same data, making it easier to build multi-container applications.
        
    3. **Separate Concerns:** Volumes separate the concerns of data storage and container execution, making it easier to manage, backup, and restore data.
        
    4. **Backup and Migration:** Volumes facilitate data backup, migration, and recovery, as the data is decoupled from the container lifecycle.
        
    5. **Performance Optimization:** Volumes can offer improved performance compared to bind mounts, especially when dealing with large amounts of data.
        
    6. **Driver Support:** Docker provides various types of volume drivers, allowing you to choose the best storage solution for your needs, such as local, remote, or cloud-based storage.
        

In summary, Docker volumes provide a way to manage data persistence, sharing, and separation in containers, enhancing the flexibility, scalability, and manageability of your Dockerized applications.

## What are docker volumes?

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691468329437/366219d4-d396-41a6-90f6-f846d259044f.png align="center")

* Docker volumes are a mechanism for managing and persisting data in Docker containers.
    
* They allow you to separate the storage of data from the lifecycle of containers, providing a way to store, share, and manage data in a more controlled and flexible manner.
    
* In Docker, containers are designed to be lightweight and ephemeral, meaning they can be started, stopped, and destroyed without affecting the underlying data.
    
* However, some applications require persistent data that should survive container restarts, updates, or removals.
    
* Docker volumes address this need by providing a way to manage data independently of the container instances.
    

## What is Docker Network?

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691468461010/52d4ae4e-7274-4730-afe1-3524bcbda1c2.png align="center")

* Docker networks are a fundamental feature of Docker that enables communication and connectivity between containers, as well as between containers and the host system.
    
* Docker networks provide an isolated environment for containers to interact with each other while maintaining security and segregation.
    
* To create and manage Docker networks, you can use the Docker CLI commands like **docker network create, docker network ls and docker network connect**. You can also define networks in Docker Compose YAML files.
    
* Docker networks play a crucial role in facilitating communication and connectivity between containers, both within a single host and across multiple hosts.
    
* They provide an essential tool for building and deploying complex applications composed of multiple interacting containers.
    

## Task-1

**Create a multi-container docker-compose file that will bring *UP* and bring *DOWN* containers in a single shot ( Example - Create application and database container )**

Here is an example of a Docker Compose file that defines a multi-container setup with an application container and a database container. This example includes configurations for both bringing the containers up and bringing them down using a single command.

Create a file named **docker-compose.yml** and add the following content:

```plaintext
version: '3.8'
services:
  app:
    image: nginx:latest
    ports:
      - "8080:80"
    depends_on:
      - db

  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: app_database
```

* We can also adjust the port mappings, environment variables, and other settings as needed.
    
* To bring up the containers, navigate to the directory containing the ***docker-compose.yml* file** in your terminal and run:
    
    ```plaintext
    docker-compose up -d
    ```
    
    This command will start both the application and database containers in detached mode (-d), allowing them to run in the background.
    
    To bring down the containers, use the following command:
    
    ```plaintext
    docker-compose down
    ```
    
    This command will stop and remove the containers, along with any associated networks and volumes.
    

## Task-2

* **how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.**
    
    1. **Create a Docker Volume (Named Volume):**
        
    
    You can create a named volume using the **docker volume create** command. This volume will store the shared data that can be accessed by multiple containers.
    
    ```plaintext
    bashCopy codedocker volume create my-shared-data
    ```
    
    1. **Create Containers Using the Named Volume:**
        
        When creating containers, you can specify the named volume to be mounted into the containers. This allows them to share data using the same volume.
        
        ```plaintext
        docker run -d --name container1 -v my-shared-data:/shared-data my-image1
        docker run -d --name container2 -v my-shared-data:/shared-data my-image2
        ```
        
        In this example, my-image1 and my-image2 are the images of the containers you want to create. The -**v** flag specifies the volume to be mounted into the containers.
        
    2. **Access Shared Data:**
        
        Inside the containers, the /shared-data directory will contain the shared data from the named volume. Both container1 and container2 will have access to the same data.
        

Here's how you can achieve the same using Docker Compose:

**Create a Docker Compose YAML File:**

Create a **docker-compose.yml** file that defines the named volume and the containers that use it.

```plaintext
version: '3.8'
services:
  container1:
    image: my-image1
    volumes:
      - my-shared-data:/shared-data

  container2:
    image: my-image2
    volumes:
      - my-shared-data:/shared-data

volumes:
  my-shared-data:
```

1. **Run Docker Compose:**
    
    Navigate to the directory containing the `docker-compose.yml` file and run:
    
    ```plaintext
    bashCopy codedocker-compose up -d
    ```
    
    This command will create and start the containers using the named volume.
    
2. **Access Shared Data:**
    
    Inside the containers, you can access and modify the shared data in the **/shared-data directory.**
    

Using Docker volumes and named volumes as described above, you can easily share files and directories between multiple containers. This is particularly useful for scenarios where you need data consistency and synchronization across different parts of your application.

1. **Create two or more containers that read and write data to the same volume using the docker run --mount command.**
    
    **Create a Docker Volume:**
    
    Let's start by creating a Docker volume that the containers will use to share data.
    
    ```plaintext
    docker volume create my-shared-volume
    ```
    
    **Create Containers:**
    
    Now, create two containers that will read and write data to the shared volume using the docker run --mount command.
    
    ```plaintext
    # Container 1
    docker run -d --name container1 --mount source=my-shared-volume,target=/shared-data busybox sh -c "echo 'Hello from Container 1' > /shared-data/message.txt"
    
    # Container 2
    docker run -d --name container2 --mount source=my-shared-volume,target=/shared-data busybox sh -c "cat /shared-data/message.txt"
    ```
    
    In this example, we're using the **busybox image**, a lightweight base image. The first container writes a message to a file in the shared volume, and the second container reads and displays the message.
    
    **Access Shared Data:**
    
    You can check the output of the second container to verify that it successfully read the message from the shared volume.
    
    ```plaintext
    docker logs container2
    ```
    
    The output should display: "Hello from Container 1".
    
    This example demonstrates how to create two containers that interact with the same volume using the docker run **\--mount** command. You can adapt this approach to more complex scenarios and use cases, where multiple containers need to share and manipulate data in a coordinated manner.
    
2. **Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.**
    
    **Verify Data Using docker exec:**
    
    Use the docker exec command to run a command inside each container and verify that the data is the same:
    
    ```plaintext
    # Verify data in Container 1
    docker exec container1 cat /shared-data/message.txt
    
    # Verify data in Container 2
    docker exec container2 cat /shared-data/message.txt
    ```
    
    1. **Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.**
        
    
    **List All Volumes:**
    
    Use the **docker volume** **ls** command to list all volumes created on your system:
    
    ```plaintext
    docker volume ls
    ```
    
    This will display a list of volumes along with their names and other information.
    
    **Remove the Volume:**
    
    Once you're done testing and want to remove the volume you created, you can use the **docker volume rm** command followed by the **volume name**.
    
    In our previous examples, the volume name is **my-shared-volume:**
    
    ```plaintext
    docker volume rm my-shared-volume
    ```
    
    Note that you can only remove a volume if there are no containers or services that are actively using it. If you attempt to remove a volume that is still in use, you will receive an error message.
    
    Remember that removing a volume is a permanent action and will result in the loss of any data stored in that volume. Make sure you've backed up any important data before removing a volume.
    
    By using the **docker volume ls** and **docker volume rm** commands, you can easily manage and clean up volumes that are no longer needed in your Docker environment.
    

## Conclusion:

Docker volumes are a crucial feature that enables containers to manage data persistence, sharing, and separation. They provide a mechanism for containers to store and access data independently of their lifecycle, enhancing application reliability, scalability, and portability.

By isolating data from the container filesystem, volumes enable data consistency, easy backup, and seamless data sharing between containers. Docker volumes empower developers to design versatile applications that can efficiently manage and communicate data, making them an essential tool in the Docker ecosystem.

**I hope you find the blog post helpful! If you do, don't forget to like 👍, comment 💬, and share 📢 it with your friends and colleagues.**
