Docker Compose
Table of Contents
What is Docker Compose?
Docker Compose lets you define and run multi-container applications from a single YAML file. Instead of multiple docker run commands, you describe your stack in docker-compose.yml and bring it up with one command.
Minimal example
Create a file docker-compose.yml:
services:
web:
image: nginx:alpine
ports:
- "8080:80"
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:From that directory:
docker compose up -dYou now have nginx on port 8080 and Postgres with a persistent volume. To stop and remove everything:
docker compose downWhy use Compose?
- One command —
upanddownfor the whole stack. - Networking — Containers can talk to each other by service name (e.g.
db). - Reproducibility — Same file runs the same stack anywhere.
- Handy for homelab — Ideal for self-hosted apps (web + database + cache) without custom scripts.
Tips
- Use
docker compose logs -fto follow logs. - Add
restart: unless-stoppedso containers come back after a reboot. - Keep secrets out of the file; use env files or a secrets manager.
Once you’re comfortable with Compose, you can start building custom images and stacking more services.