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

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:

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.

Answer-2:

  • Command to create containers with shared volume:

  •         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
    

  • Verify data consistency:

  •         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:

docker volume ls
docker volume rm my_volume