DevOps Engineering Containers08 / 21

SECTION 03 · 35 MIN READ & EXPLORE

Docker Compose

Replace manual container commands with declarative service, network, and volume definitions in YAML.

Why Use Docker Compose?

Managing multi-container applications with manual docker run commands quickly becomes error-prone: every network, volume, port mapping, and environment variable must be typed in the exact right order. Docker Compose captures the entire application stack in a single declarative YAML file (docker-compose.yml) that creates a shared bridge network, connects services by name, manages volumes, and starts or stops everything in one command.

EXPLORE THE DIAGRAM

From manual CLI commands to declarative Docker Compose

Experiment freely. Changes disappear when you reset or refresh.

Preview of From manual CLI commands to declarative Docker Compose

Why and When to Use Compose Stacks?

Use Docker Compose for local development, reproducible multi-service demos, integration testing, and single-host deployments where multiple containers work together.

Explore the files

docker-compose.yml

version: '3.8'

services:
  # The Database Service
  db:
    image: mysql:8.0
    container_name: can2025-db
    ports:
      - "127.0.0.1:3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: can2025
    volumes:
      - db_data:/var/lib/mysql

  # The Web Interface Service
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: can2025-admin
    links:
      - db
    ports:
      - "127.0.0.1:8080:80"
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root

volumes:
  db_data:

dev.yml

version: '3.8'

services:
  countdown-app:
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: can2025-countdown
    ports:
      - "127.0.0.1:5000:5000"
    environment:
      - PLAYER_NAME=Yassine Bono
      - PLAYER_NUMBER=1

wordpress.yml

version: '3.8'

services:
  wordpress:
    image: wordpress:latest
    ports:
      - "127.0.0.1:8000:80"
    environment:
      # We tell WordPress where the DB is (Service Name:Port)
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: wp_password
      WORDPRESS_DB_NAME: press_portal
    volumes:
      - wp_content:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:8.0
    # FIX: Force MySQL 8 to use the standard password method so PHP can connect easily
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: press_portal
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: wp_password
      MYSQL_ROOT_PASSWORD: root_secret_password
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wp_content:
  db_data:

Dockerfile

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app.py .

EXPOSE 5000

CMD ["python", "app.py"]
SCENARIOS

Running three multi-container Compose stacks

Inspect & Start the Database Stack

configup -dpslogsdown
SITUATION

Instead of running separate docker network, docker volume, and docker run commands for MySQL and phpMyAdmin, both services are declared in docker-compose.yml.

TASK

Validate the Compose file, launch the stack in detached mode, inspect logs, open phpMyAdmin, and tear it down.

# 1. Validate and render the resolved Compose configuration
docker compose config

# 2. Start MySQL and phpMyAdmin in detached mode
docker compose up -d

# 3. Check service status and inspect logs
docker compose ps
docker compose logs

# 4. Open http://localhost:8080 once MySQL is ready, then stop the stack
docker compose down

WHAT TO OBSERVE

Compose automatically creates a project network and the db_data volume, connects phpmyadmin to db, and removes containers and the network on docker compose down while preserving db_data.

Build & Run the Development App

-f dev.ymlup --build
SITUATION

Compose can also build local Dockerfiles on demand and inject environment variables directly from YAML.

TASK

Enter the dev folder, build and launch the countdown service from dev.yml, and verify the configured player appears.

# 1. Enter the dev directory and build + start the service
cd dev
docker compose -f dev.yml up --build

# 2. Open http://localhost:5000 in your browser

# 3. Press Ctrl+C to stop, remove the container, and return to the parent folder
docker compose -f dev.yml down
cd ..

WHAT TO OBSERVE

Compose builds dev/Dockerfile, injects PLAYER_NAME=Yassine Bono and PLAYER_NUMBER=1, and serves the page on http://localhost:5000.

Run the WordPress Multi-Service Stack

-f wordpress.ymlup -ddown --volumes
SITUATION

A real CMS stack pairs WordPress with MySQL, separate persistent volumes for database and site content, and service-to-service DNS.

TASK

Start the WordPress stack from wordpress.yml, verify the setup screen on localhost:8000, and clean up including volumes when done.

# 1. Start WordPress and MySQL together
docker compose -f wordpress.yml up -d

# 2. Verify both containers are running
docker compose -f wordpress.yml ps

# 3. Open http://localhost:8000 in your browser

# 4. Stop the stack and remove example volumes when finished
docker compose -f wordpress.yml down --volumes

WHAT TO OBSERVE

WordPress connects to db:3306 over the Compose network and displays the WordPress installation page at http://localhost:8000. Passing --volumes to down explicitly deletes wp_content and db_data.

Quick Reference

CHEAT SHEET

Docker Compose CLI & YAML keys

Essential commands for managing Compose stacks and the top-level YAML fields you will write most often.

Compose Lifecycle CLI

docker compose config
Validate and print the resolved YAML config
READ
docker compose up -d
Create and start all services in background
CREATE
docker compose up --build
Rebuild local images before starting services
UPDATE
docker compose ps
List containers belonging to the stack
READ
docker compose logs -f
Follow combined logs across all services
DEBUG
docker compose down
Stop and remove containers & network (keeps volumes)
DELETE

Key Compose YAML Fields

services:
Declares each container; keys become DNS hostnames
CORE
build: / image:
Build a local Dockerfile or pull a registry image
IMAGE
ports:
Map host ports to container ports (HOST:CONT)
NET
environment:
Inject runtime environment variables
CONFIG
volumes:
Attach named volumes or bind mounts
DATA
depends_on:
Control container startup order (not readiness)
ORDER
A LITTLE CLEARER?

Take a moment to explain the idea in your own words.

Progress is saved in this browser.

DevOps Engineering

Course overview ↗

01Introduction to DevOps

  1. What is DevOps?20m
  2. DevOps Roles20m