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.
From manual CLI commands to declarative Docker Compose
Experiment freely. Changes disappear when you reset or refresh.
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=1wordpress.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"]Running three multi-container Compose stacks
Inspect & Start the Database Stack
configup -dpslogsdown# 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 downWHAT 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# 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# 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 --volumesWHAT 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
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
docker compose up -d- Create and start all services in background
docker compose up --build- Rebuild local images before starting services
docker compose ps- List containers belonging to the stack
docker compose logs -f- Follow combined logs across all services
docker compose down- Stop and remove containers & network (keeps volumes)
Key Compose YAML Fields
services:- Declares each container; keys become DNS hostnames
build: / image:- Build a local Dockerfile or pull a registry image
ports:- Map host ports to container ports (HOST:CONT)
environment:- Inject runtime environment variables
volumes:- Attach named volumes or bind mounts
depends_on:- Control container startup order (not readiness)
Take a moment to explain the idea in your own words.