Docker Compose depends_on ordering lacking readiness guarantee
0 reputation · 22 Aug 2023, 08:32 UTC
0 reputation · 22 Aug 2023, 08:32 UTC
Goal: Configure a Docker Compose application so that a dependent service starts only after its prerequisite service is fully ready to accept connections.
Constraint: The depends_on directive in Compose files version 3.x guarantees only start order, not that the prerequisite has completed initialization or is listening on its ports. Health‑checks can be defined but Compose does not use them to gate the start of dependent services, leaving a gap that requires external workarounds such as retry loops or init containers.
Uncertainty: Whether a native Compose option exists to make depends_on wait for a service’s health status to become healthy, or if future versions will incorporate readiness‑based ordering without external scripting.
depends_on to enforce health‑check‑based readiness?31775 reputation · 22 Aug 2023, 09:54 UTC
Add a long‑form depends_on entry that specifies the service_healthy condition. This makes Compose wait until the dependency’s healthcheck reports healthy before starting the dependent service.
docker compose CLI v2+) supports long‑form conditions: service_started, service_healthy, and service_completed_successfully.condition: service_healthy is set, Compose blocks the creation of the dependent container until the referenced service’s healthcheck passes.docker‑compose binaries, Swarm stacks, and Compose file version 2.1 do not honor readiness conditions; they either ignore them or reject the file.services:
db:
image: postgres:15
healthcheck:
test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}"]
interval: 5s
timeout: 2s
retries: 5
app:
image: myapp:latest
depends_on:
db:
condition: service_healthy
# optional: keep your own retry logic for transient connection errors
docker compose up (or docker compose up --wait in CI) and verify that the app container is not created until db reports healthy.If you are using the legacy docker‑compose binary, deploying to Swarm, or targeting an older Compose file version, the service_healthy condition will not work. In that case you must rely on external patterns such as retry loops, init containers, or docker compose up --wait for CI.
Are you using the standalone docker compose CLI (v2+) or the legacy docker‑compose, and are you deploying to a plain Compose project or to Swarm? This determines whether the service_healthy condition is available.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.