/config/docker-compose/depends-on-condition-service-healthy
Compose depends_on condition: service_healthy with a healthcheck
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Docker Compose version v5.6.0updated 2026-10-20
- tool
- Docker Compose
- file
- compose.yaml
- key
- services.<name>.depends_on.<service>.condition
- type
- service_started | service_healthy | service_completed_successfully
- default
- service_started
A plain depends_on: [db] only waits until the db container has started, not until the database accepts connections. With the long syntax and condition: service_healthy, Compose waits until the dependency's healthcheck reports healthy before it creates the dependent service.
Example
cat > compose.yaml <<'EOF'
name: demo
services:
web:
image: nginx:1.29
depends_on:
db:
condition: service_healthy
restart: true
db:
image: postgres:18
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
EOF
docker-compose configOutput
name: demo
services:
db:
image: postgres:18
networks:
default: null
healthcheck:
test:
- CMD-SHELL
- pg_isready -U postgres
timeout: 3s
interval: 5s
retries: 10
start_period: 10s
web:
image: nginx:1.29
networks:
default: null
depends_on:
db:
condition: service_healthy
restart: true
required: true
networks:
default:
name: demo_defaultdocker compose config prints the resolved configuration, including the defaults Compose fills in (required: true, the default network).
Conditions
| condition | Waits until |
|---|---|
| service_started | the container has started (default, same as the short syntax) |
| service_healthy | the healthcheck of the dependency reports healthy |
| service_completed_successfully | the dependency ran to completion with exit code 0 (migrations, seed jobs) |
Related keys
restart: truerestarts the dependent service when the dependency is restarted by a Compose operation.required: falselets the project start when the dependency is not defined (Compose 2.20+).- The healthcheck command runs inside the dependency's container, so the binary (
pg_isready,curl,redis-cli) must exist in that image.
Sources
Related pages
errors · docker-composeDocker Compose: service "web" depends on undefined service "db"docker compose fails with service "web" depends on undefined service "db" when depends_on names a service missing from the file, the -f list or a profile.changes · docker-composedocker-compose v1 to docker compose v2: what changedCompose v2 replaces the Python docker-compose with the docker compose plugin: version is obsolete, container names use hyphens, and v1 is end of life.codes · dockerDocker container exit codes: 125, 126, 127, 137, 139, 143Docker exit codes explained: 125 is a docker run error, 126 and 127 are command problems, 137 is SIGKILL or out of memory, 143 SIGTERM and 139 a segfault.