You will describe a web + database + cache stack in a single Compose file and launch it with one command. By the end you will have reproducible, ordered startup with health checks instead of fragile manual docker run sequences.

Learning Objectives

  • Define services, networks, and volumes declaratively.
  • Order startup with depends_on and healthchecks.
  • Manage the stack lifecycle with compose commands.
  • Time: ~3 hours · Difficulty: Intermediate · Prereqs: Docker Compose v2.

Architecture Overview

Environment Setup

You will need: Docker Compose v2 (docker compose version).

Before you begin: create a project directory for the compose file.

Step-by-Step Execution

01
Write the compose file

Declaring services with a healthcheck and depends_on guarantees the DB is ready before the web app starts.

# compose.yaml
services:
  db:
    image: postgres:16
    environment: { POSTGRES_PASSWORD: secret }
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
  web:
    image: nginx:stable
    depends_on:
      db: { condition: service_healthy }
    ports: ["8080:80"]
02
Bring the stack up
docker compose up -d
Creates the network, volumes, and starts services in dependency order.
03
Check status and logs
$ docker compose ps
NAME SERVICE STATUS PORTS proj-db db Up (healthy) proj-web web Up 0.0.0.0:8080->80/tcp

Progress So Far

Testing & Validation

curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080 && docker compose down

You should see 200, then a clean teardown. If so, your whole stack is reproducible from one file.

Troubleshooting
  • web starts before db is ready: use a healthcheck with condition: service_healthy.
  • Old containers conflict: run docker compose down before re-creating.
  • Env not applied: confirm a .env file or environment keys are present.

Extension Ideas

Key Results

  • Launched a 3-service stack with a single command.
  • Guaranteed startup order with healthchecks and depends_on.
  • Made the environment reproducible from one declarative file.
  • Verified the app and a clean teardown.