Skip to content

depends_on and healthchecks

Select an icon to see what it is.

Step 6 of 6

An unhealthy dependency stops the project

If the check fails retries times in a row, the status becomes unhealthy. Compose then stops and prints an error, and the services that depend on db are never started. You see the failure instead of a half-working app.

PrevNext

docker inspect (State.Health)

{
"State": {
"Health": {
"Status": "unhealthy"
"FailingStreak": 5
"Log": [
{
"ExitCode": 2
"Output": "/var/run/postgresql:5432 - no response"
}
]
}
}
}

Try it yourself

Add a healthcheck to db, wait for it in api, and watch the status change.

$ cat > compose.yaml <<'EOF' name: shop services: web: image: nginx ports: - "8080:80" depends_on: - api networks: default: ipv4_address: 172.18.0.2 api: image: traefik/whoami command: ["--port", "3000"] depends_on: db: condition: service_healthy networks: default: ipv4_address: 172.18.0.3 db: image: postgres:17 environment: POSTGRES_PASSWORD: example healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 3s retries: 5 volumes: - pgdata:/var/lib/postgresql/data networks: default: ipv4_address: 172.18.0.4 volumes: pgdata: networks: default: name: shop-net ipam: config: - subnet: 172.18.0.0/16 EOF
$ docker compose up -d
$ docker inspect --format '{{.State.Health.Status}}' shop-db-1
$ docker compose ps
$ docker compose down

Quiz

1. Where does the healthcheck command run?
2. What does a plain depends_on wait for?
3. What happens when a dependency becomes unhealthy during up?