Sub topic 8
Making a service wait for a healthy dependency
This chapter assumes you can read a compose.yaml file, run docker compose up and docker compose down, and know what a container image is. It also assumes you have seen a healthcheck definition with test, interval, timeout, retries, and start_period.
The problem to solve
The short form of depends_on only controls the order in which containers are created and started. Compose starts db, then starts web right away. PostgreSQL may still be initializing, so web can fail on its first connection attempt.
The fix has two parts. First, give the dependency a healthcheck so Compose can tell when it is ready. Second, change depends_on to the long syntax and ask for condition: service_healthy.
Step 1: Define the healthcheck on the dependency
The database needs a command that succeeds only when it accepts connections. For PostgreSQL, pg_isready does this.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10sCompose runs this command inside the db container every 5 seconds. Failures during the 10-second start_period do not count toward the retry limit.
Step 2: Wait on the health status
Now add web and use the long syntax of depends_on. The service name becomes a key, and condition goes under it.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
web:
image: myapp:latest
depends_on:
db:
condition: service_healthyWith this file, Compose starts db first. It then waits until db reports healthy, and only then starts web.
What happens during startup
Conditions at a glance
The long syntax accepts three conditions.
| Condition | Compose waits until the dependency... |
|---|---|
| `service_started` | has started. This is the same as the short syntax. |
| `service_healthy` | reports `healthy`. |
| `service_completed_successfully` | has run to completion and exited with code 0. |
service_healthy only works if the dependency defines a healthcheck. Without one, the container never reports a health status, so there is nothing to wait for.
Common errors
- No healthcheck on the dependency. Compose cannot evaluate
service_healthy. Add ahealthcheckblock to the dependency service. - The health command is missing from the image. If the image does not contain
pg_isready(orcurl,wget, and so on), the check always fails and the service ends upunhealthy. Pick a command that exists in that image. - The dependency never becomes healthy. Compose stops the startup and reports that the dependency failed.
webis never started. Check the dependency's logs and its health command. - Mixing up the syntaxes. You cannot put a
conditionon the list form (- db). Switch to the mapping form shown above.
Try it
Start the stack and wait for it to settle:
docker compose up -d --wait
docker compose psIn docker compose ps, db should show healthy and web should show as running. Run docker compose down when you are done.
Exercise
A teammate changes depends_on for web to condition: service_healthy, but db has no healthcheck block. What is the problem, and how do you fix it?
Show answer
Compose has no health status to wait for, because db never reports healthy, so the dependency condition cannot be met. Add a healthcheck to db, for example test: ["CMD-SHELL", "pg_isready -U postgres"] with interval, timeout, retries, and start_period.