- Intermediate
- Docker Compose
How it works
Each service is a container definition: an image or a build:, ports, environment, volumes, restart, healthcheck. Compose puts every service on the project's network and recreates only the containers whose configuration changed. docker compose down removes containers and networks but keeps named volumes, unless you add -v.
What breaks
YAML is strict: indentation decides the structure, and port mappings like "22:22" must be quoted or YAML may read them as a number. docker compose config prints the file as Compose understands it, after variables and overrides.
In compose.yaml
services:
api:
build: ./api
environment:
DATABASE_HOST: db
restart: unless-stopped
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
See it break, then fix it
Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.