Docker Compose — Introduction Exercises

2 minute read

Practice what you’ve learned in the Introduction chapter. These exercises cover Docker Compose fundamentals, V1 vs V2, use cases, and the Compose ecosystem.


Exercise 1 — Compose Elevator Pitch

Scenario: A colleague asks you “What is Docker Compose and why should I use it?” Write a 3-sentence explanation.

Show Answer

Docker Compose is a tool that lets you define all the containers your application needs in a single YAML file called compose.yml. Instead of running multiple docker run commands with complex flags, you describe your services, networks, and volumes declaratively and bring everything up with docker compose up. It’s perfect for local development, testing, and small deployments where you need multiple containers working together.


Exercise 2 — V1 vs V2 Migration

Question: Convert these V1 commands to V2 commands:

docker-compose up -d
docker-compose logs -f web
docker-compose exec db psql -U postgres
docker-compose down -v
docker-compose --version
Show Answer
docker compose up -d
docker compose logs -f web
docker compose exec db psql -U postgres
docker compose down -v
docker compose version

Key changes:

  • Hyphen (-) becomes a space ( )
  • --version becomes version (subcommand style)

Exercise 3 — Use Case Decision

Scenario: For each situation, decide if Docker Compose is the right tool. Answer Yes or No and explain why.

  1. Running a WordPress blog with MySQL locally for development
  2. Deploying 50 microservices across 20 servers with auto-scaling
  3. Running integration tests in a CI pipeline with a database and Redis
  4. Setting up a quick Nginx container to serve static files
  5. Sharing a complete development environment with your team
Show Answer
  1. Yes — Classic Compose use case: 2 services, local development
  2. No — Multi-host, auto-scaling = Kubernetes territory
  3. Yes — Compose excels at reproducible test environments in CI
  4. No — Single container, docker run -p 8080:80 nginx is simpler
  5. Yes — Share compose.yml in Git, everyone runs docker compose up

Exercise 4 — Container Naming

Question: Given this compose file in a directory called myproject:

services:
  web:
    image: nginx
  api:
    image: node:20
  db:
    image: postgres:16

What will the container names be in Compose V2? What network will be created?

Show Answer

Container names:

  • myproject-web-1
  • myproject-api-1
  • myproject-db-1

Network created:

  • myproject_default

In V2, container names use hyphens (-) and the project name defaults to the directory name.


Exercise 5 — Ecosystem Matching

Question: Match each tool to its description:

# Tool Description
1 Kompose ?
2 Testcontainers ?
3 Podman Compose ?
4 Docker Swarm ?
5 Awesome Compose ?

Options:

  • A. Multi-host orchestration using compose files
  • B. Collection of example compose files on GitHub
  • C. Converts compose files to Kubernetes manifests
  • D. Rootless Compose alternative using Podman
  • E. Programmatic container management for testing
Show Answer
  1. Kompose → C — Converts compose files to Kubernetes manifests
  2. Testcontainers → E — Programmatic container management for testing (Java, Go, Python, .NET)
  3. Podman Compose → D — Rootless Compose alternative using Podman engine
  4. Docker Swarm → A — Multi-host orchestration using compose files (docker stack deploy)
  5. Awesome Compose → B — Collection of example compose files on GitHub (github.com/docker/awesome-compose)