Compose depends_on with condition service_healthy waits for a dependency's healthcheck
Answer: Use the long form of depends_on with condition: service_healthy and give the dependency a healthcheck. Plain depends_on only controls order: Compose waits until a dependency is running, not until it is "ready".
Example
services:
web:
build: .
depends_on:
db:
condition: service_healthy
restart: true
redis:
condition: service_started
redis:
image: redis
db:
image: postgres:18
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 10s
retries: 5
start_period: 30s
timeout: 10s
Conditions
| Condition | Specifies | |---|---| | service_started | the dependency has started (the short syntax behaves this way) | | service_healthy | the dependency is "healthy", as defined with healthcheck, before the dependent starts | | service_completed_successfully | the dependency runs to successful completion first |
Other options
restart: truerestartswebautomatically ifdbis updated or restarted by an explicit Compose operation such asdocker compose restart.required: falsemakes Compose only warn when the dependency isn't started or available; the default istrue. It was introduced in Docker Compose version 2.20.0.
Ordering rules
Compose always starts and stops containers in dependency order, where dependencies come from depends_on, links, volumes_from and network_mode: "service:...". Here db and redis are created before web, and web is removed before them. Without the service_healthy condition, an application might start before the database and won't find a database able to handle its SQL statements.
See also
Claims
- id: C1 claim: Compose waits only until a container is running, not ready. source: S1 evidence: "Compose does not wait until a container is \"ready\", only until it's running."
- id: C2 claim: required defaults to true. source: S2 evidence: "If it's not defined the default value of required is true ."
- id: C3 claim: Compose removes services in dependency order. source: S1 evidence: "Compose also removes services in dependency order."
Sources
- [S1] Control startup and shutdown order in Compose. Docker docs. https://docs.docker.com/compose/how-tos/startup-order/ . License: Apache-2.0. Accessed 2026-11-18.
- [S2] Services top-level elements. Docker docs (Compose file reference). https://docs.docker.com/reference/compose-file/services/ . License: Apache-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker docs, Control startup and shutdown order in Compose; Docker docs (Compose file reference), Services top-level elements. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker docs, Control startup and shutdown order in Compose; Docker docs (Compose file reference), Services top-level elements. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Compose depends_on with condition service_healthy waits for a dependency's healthcheck." iinventedlight Library, iil:note:s3sckcaf, version of 2026-11-18. https://iinventedlight.com/library/notes/docker-compose-depends-on/. License: CC-BY-4.0.
@misc{iil_note_s3sckcaf,
author = {{iinventedlight}},
title = {{Compose depends\_on with condition service\_healthy waits for a dependency's healthcheck}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/docker-compose-depends-on/},
note = {Permanent ID iil:note:s3sckcaf. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- docker-compose-depends-on.table.json (table; CC-BY-4.0)
- docker-compose-depends-on.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/docker-compose-depends-on.md.