Docker Compose and Volumes Mastery

Docker Compose and Volumes Mastery

Introduction

Welcome back to another day of the 90DaysOfDevOps challenge! Today, we're diving deep into the world of Docker Compose and Volumes. These powerful tools will help us orchestrate multi-container applications seamlessly and efficiently share data between containers.

Task 1: Docker Compose Magic

Step 1: Create a Multi-Container Docker-Compose File

To start, we'll create a docker-compose.yml file to define our multi-container setup. This example will include an application and a database container. Ensure Docker Compose is installed on your system.

version: '3'
services:

  backend:
    build:
      context: .
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: myDb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin

Replace your-app-image and your-db-image with your actual application and database image names. This file specifies two services, app and database, with the necessary configurations.

Step 2: Docker-Compose Up and Down

Now, run the following commands to bring up and bring down your containers:

# Bring up containers in detached mode
docker-compose up -d

# View container status
docker-compose ps

# View container logs (replace 'app' with your service name)
docker-compose logs app

# Bring down containers and clean up
docker-compose down

Using the -d flag with docker-compose up starts the containers in detached mode, allowing you to continue working in the same terminal.

Task 2: Mastering Docker Volumes

Step 1: Creating and Sharing Volumes

Now, let's explore Docker Volumes and Named Volumes. Update your docker-compose.yml file to include a volume for data sharing:

version: '3'
services:

  backend:
    build:
      context: .
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: myDb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    volumes:
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql   # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
      - mysql-data:/var/lib/mysql  # Mount the volume for MySQL data storage

volumes:
  mysql-data:

This modification introduces two named volumes, app-data and db-data, which are mounted to specific paths in the containers.

Step 2: Verifying Data Sharing

Run the following commands to verify that the data is shared between containers:

# Bring up containers
docker-compose up -d

# Execute a command inside the 'app' container to create a file
docker exec -it your_app_container_id touch /app/data/shared_file.txt

# Execute a command inside the 'database' container to check the file's existence
docker exec -it your_db_container_id ls /var/lib/mysql/shared_file.txt

# Bring down containers
docker-compose down

Replace your_app_container_id and your_db_container_id with the actual container IDs obtained from docker-compose ps.

Step 3: Cleaning Up

After verifying the data sharing, you can clean up by removing the volumes:

# List all volumes
docker volume ls

# Remove the volumes
docker volume rm app-data db-data

Docker Compose and Volumes are essential tools for orchestrating multi-container applications and efficiently sharing data between them. Stay tuned for more DevOps adventures in the upcoming days!