Sub topic 11
Waiting for dependencies with service_healthy
This chapter assumes you can write a Compose file, that you know how a healthcheck block defines a probe, and that you know the starting, healthy and unhealthy states from earlier chapters.
The problem: started is not ready
A web service often needs its database before it can do useful work. The short form of depends_on only controls start order:
services:
web:
build: .
depends_on:
- dbCompose starts db, waits for its container to be created and started, then starts web. A database such as PostgreSQL can take several seconds after that to initialize and accept connections. If web connects during that gap, it fails with "connection refused".
The short form is shorthand for condition: service_started. That condition only checks that the container process exists. It does not check that the application is ready.
Using service_healthy
The long form of depends_on accepts a condition. With service_healthy, Compose waits until the dependency's health check reports healthy before it starts the dependent service.
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:
build: .
depends_on:
db:
condition: service_healthyTwo parts must both be present:
- The
dbservice needs a health check, either in this file or inherited from the image. - The
webservice needs thecondition: service_healthyline.
If db has no health check at all, Compose cannot ever see it as healthy. It reports an error instead of waiting.
The three conditions
| Condition | Compose waits until |
|---|---|
| `service_started` | The dependency's container has started. This is the default. |
| `service_healthy` | The dependency's health check reports `healthy`. |
| `service_completed_successfully` | The dependency has run to completion and exited with code 0. |
The last condition suits one-shot jobs, such as a migration container that must finish before the application starts.
What the sequence looks like
You can watch this with docker compose up -d in one terminal and docker compose ps in another. The db row shows (health: starting) and then (healthy). The web container appears only after that.
Timing and failure
The wait is bounded by the dependency's own settings. A probe runs every interval. Failures during start_period do not count toward retries. After retries consecutive failures outside that window, the container becomes unhealthy. Compose then stops waiting and docker compose up exits with an error such as dependency failed to start: container myproject-db-1 is unhealthy. Set start_period to cover the slowest normal startup, or a slow but working database will cause a failed up.
You can also run docker compose up --wait. It returns only once all services are running and healthy, which is useful in scripts and CI.
Limits to remember
- The check gates
docker compose upordering. It is not a supervisor. - A shallow probe gives false confidence.
pg_isreadyconfirms the server accepts connections, but it does not confirm that your schema exists. - Keep the probe cheap. It runs every
intervalfor the life of the container.
Exercise
Your web service starts before PostgreSQL accepts connections, even though you wrote depends_on: [db] and db has a working pg_isready health check. What two changes fix this, and why did the original not work?
Show answer
The short form of depends_on means condition: service_started, so Compose only waited for the db container to start, not for the health check to pass. Change it to the long form, depends_on: db: condition: service_healthy. The db service must also define a healthcheck, which it already does here. Optionally add a start_period on that check so slow initialization does not count as failures. Compose then starts web only after db reports healthy.