Sub topic 9
Health checks and depends_on conditions
This chapter assumes you can read a compose.yaml, know that services reach each other by service name on the Compose network, and have run docker compose up. It covers how to make Compose wait until a dependency is ready, not only started.
Started is not ready
A plain depends_on list only controls start order. Compose starts db before web, but it does not wait for Postgres to accept connections. The database process needs a few seconds to initialize, and a web service that connects immediately can fail with "connection refused".
A health check closes that gap. It is a command Docker runs inside the container at intervals. The result sets the container's health status: starting, healthy, or unhealthy.
Defining a health check
This stack waits for the database before starting web.
name: shop
services:
web:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
volumes:
db-data:The healthcheck fields work like this:
testis the command. Exit code 0 means healthy and 1 means unhealthy.CMD-SHELLruns the string through the container's shell.CMDruns an argument list without a shell.intervalis the time between checks.timeoutis how long one check may run before it counts as a failure.retriesis the number of consecutive failures before the container is markedunhealthy.start_period(optional) gives slow services a grace period. Failures during it do not count towardretries.
The command must exist inside the image. If the image has no pg_isready, curl, or wget, the check always fails.
The three conditions
The long form of depends_on takes a condition:
| Condition | Compose waits until the dependency... |
|---|---|
| `service_started` | has started (the default, same as the short list form) |
| `service_healthy` | reports `healthy` |
| `service_completed_successfully` | has exited with code 0 |
Use service_completed_successfully for one-shot jobs such as a migration container that must finish before the application starts.
Common errors
service_healthywithout ahealthcheck. Compose reports an error because the dependency has no health status. Define the check in the Compose file or in the image.- A check that tests the wrong thing.
pg_isreadywithout-Uand-dcan log noise about a missing role or database. Match the user and database to your configuration. - Expecting restarts of
webwhendblater fails. Conditions apply only at startup. If the database becomes unhealthy afterward,webkeeps running. Your application should still retry connections. - A stack that never becomes ready. If the dependency stays
unhealthy,docker compose upstops with a dependency failed to start error. Rundocker compose psto see the status anddocker compose logs dbto find out why.
Exercise
A migrate service runs a schema migration and then exits. The web service must start only after the migration succeeds, and migrate itself needs db to be ready. What depends_on entries do you write for migrate and for web?
Show answer
In migrate, depend on db with condition: service_healthy. In web, depend on migrate with condition: service_completed_successfully. If migrate exits with a non-zero code, web does not start.