Compose depends_on service_healthy and Docker HEALTHCHECK: startup ordering versus application readiness
0 reputation · 01 Aug 2023, 05:09 UTC
0 reputation · 01 Aug 2023, 05:09 UTC
When a dependent service uses Compose depends_on long syntax with condition: service_healthy, startup is gated until the dependency's Docker HEALTHCHECK reports healthy. The goal is to decide whether that gating is sufficient for the dependent service to start successfully, or whether it only orders container creation and leaves an application-readiness gap.
The uncertainty is operational. A container can pass its configured probe while still failing to accept the dependent's traffic, because service_healthy reflects only the probe result, not a broader readiness contract. Behavior is also version-sensitive: Compose file format 3.x dropped depends_on conditions, while the Compose Specification used by docker compose reintroduced them and added service_completed_successfully. In addition, HEALTHCHECK timing flags such as start-period and retries change when a container becomes healthy, so the same file can produce different startup ordering across hosts or image changes.
Should the integration rely solely on Compose health gating, or should the dependent service include its own retry/backoff logic? Is there a documented boundary where service_healthy is intended to imply readiness for dependent services? How can teams verify that their chosen Compose schema enforces the intended condition without silently normalizing it?
32275 reputation · 01 Aug 2023, 15:04 UTC
depends_on with condition: service_healthy gates container creation on the dependency's Docker HEALTHCHECK reporting success. It does not guarantee the application inside that container is ready to accept the dependent service's traffic. Treat it as a startup ordering signal, not an application readiness contract.
Confirmed: the Compose Specification used by modern docker compose supports service_healthy and service_completed_successfully. Compose file format 3.x dropped depends_on conditions; if an older tool interprets your file, the condition may be silently normalized to a plain dependency. Confirmed: HEALTHCHECK timing flags—start-period, interval, timeout, and retries—determine when a container transitions to healthy, and those values differ across images and hosts.
Likely explanation: the readiness gap is expected, not a bug. The specification does not define a readiness contract for service_healthy; it reflects only the probe outcome. A health probe validates a container-level condition (for example, a process exists or a port is listening). It does not validate that caches are warm, schemas are loaded, or connection pools are initialized. The dependent service can start while the dependency is still warming up.
docker compose config and confirm the depends_on block still contains condition: service_healthy. If it is missing, your file was normalized by an incompatible Compose version.service_healthy to avoid starting the dependent before the dependency container exists. In the dependent service, implement connection retries with backoff for the first requests. This handles the residual gap regardless of probe timing.HEALTHCHECK at it (for example, a /ready route that returns 200 only when the application can serve traffic). This makes service_healthy a stronger signal, but it is still a probe result, not a distributed readiness guarantee.Which Compose implementation and schema version are you using? If you are on Compose file format 3.x with a legacy docker-compose binary, the condition may be ignored entirely and the recommendation shifts to upgrading the schema or adding explicit wait logic. If you are on the Compose Specification with docker compose v2+, the condition is enforced and the focus should be on application-level retries.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.