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:

yaml
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:

bash
docker compose up -d

You now have nginx on port 8080 and Postgres with a persistent volume. To stop and remove everything:

bash
docker compose down

Why use Compose?

  • One commandup and down for 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 -f to follow logs.
  • Add restart: unless-stopped so 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.