← All notes

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

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

Sources

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

Permanent ID: iil:note:s3sckcaf

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.},
}

Markdown

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.

All artifact files (JSON index)

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.