Skip to content

Docker Compose basics

Real applications have several parts β€” a web app, a database, maybe a cache. Docker Compose describes them all in one file.

compose.yaml
services:
web:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://app:app@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
retries: 5
volumes:
pgdata:
Terminal window
docker compose up -d # build and start everything
docker compose ps # status of each service
docker compose logs -f web
docker compose down # stop and remove containers (volumes are kept)
  • A private network where each service is reachable by its name β€” web talks to db:5432, not localhost.
  • Startup order with depends_on, and with condition: service_healthy it waits until the database actually accepts connections.
  • Named volumes so data survives down and up.

Check your understanding

1. How does the web service reach the database in this example?
2. What survives `docker compose down` in this example?