Docker Compose: Bring Up a Full Stack with One Command
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
graph LR
C[compose.yaml] -->|docker compose up| Stack
subgraph Stack
Web[web] --> DB[(db)]
Web --> Cache[(redis)]
end
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 -dCreates 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
graph LR
A[01 Write compose] -->|done| B[02 up -d]
B -->|done| C[03 ps + logs]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080 && docker compose downYou 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 downbefore re-creating. - Env not applied: confirm a
.envfile or environment keys are present.
Extension Ideas
- Split prod/dev with compose override files.
- Promote the stack to Kubernetes in Multi-Container Applications.
- Add the build + deploy to CI/CD.
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.