Reading a docker-compose.yml Configuration
5 exercises — read a realistic Docker Compose file with three services (web, db, redis). Understand depends_on, port mapping, volumes, healthchecks, and environment variable behavior.
docker-compose.yml reading guide
- services: → each named block is one container
- ports: "HOST:CONTAINER" → left = your machine, right = inside container
- depends_on: → startup order;
service_healthywaits for healthcheck to pass - volumes: → named volumes persist data; bind mounts (
./path) map host directories - environment: → env vars passed into the container; NOT shared between services automatically
0 / 5 completed
1 / 5
docker-compose.yml — web/db/redis stack
{ex.passage} What does the `depends_on` directive control for the web service in this file?
depends_on controls startup order with optional health conditions:
The web service has two depends_on entries with different conditions:
The database needs the stricter check because the app will crash immediately if it can't connect to Postgres on startup — and Postgres takes a few seconds to initialize even after the container starts. Redis typically starts faster and connection failures are less critical.
The three condition values:
The web service has two depends_on entries with different conditions:
- db: condition: service_healthy → web waits until the db passes its healthcheck (pg_isready returns success)
- redis: condition: service_started → web only waits for the redis container to start, not for Redis to be ready to accept connections
The database needs the stricter check because the app will crash immediately if it can't connect to Postgres on startup — and Postgres takes a few seconds to initialize even after the container starts. Redis typically starts faster and connection failures are less critical.
The three condition values:
- service_started → container is running (default if condition is omitted)
- service_healthy → container is running AND healthcheck passes
- service_completed_successfully → for one-off tasks that must finish before the service starts (e.g., database migrations)
db and redis, which are children of depends_on. Every level of indentation = one level deeper in the hierarchy.