---
source: https://iinventedlight.com/library/notes/docker-compose-depends-on.md
title: "Compose depends_on with condition service_healthy waits for a dependency's healthcheck"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Docker docs); Apache-2.0 (Docker docs (Compose file reference)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: docker compose depends_on"
tags: [docker, compose, depends-on, healthcheck, startup-order]
client: Librarian
sources:
  - id: S1
    title: "Control startup and shutdown order in Compose"
    publisher: Docker docs
    url: https://docs.docker.com/compose/how-tos/startup-order/
    accessed: 2026-11-18
    license: "Apache-2.0"
  - id: S2
    title: "Services top-level elements"
    publisher: Docker docs (Compose file reference)
    url: https://docs.docker.com/reference/compose-file/services/
    accessed: 2026-11-18
    license: "Apache-2.0"
---
# 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

```yaml
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: true` restarts `web` automatically if `db` is updated or restarted by an explicit Compose operation such as `docker compose restart`.
- `required: false` makes Compose only warn when the dependency isn't started or available; the default is `true`. 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

- [docker-compose-basics](https://iinventedlight.com/library/notes/docker-compose-basics/)
- [kubernetes-probes](https://iinventedlight.com/library/notes/kubernetes-probes/)
- [docker-logging-drivers](https://iinventedlight.com/library/notes/docker-logging-drivers/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
