Skip to content

/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

bash
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 config

Output

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_default

docker compose config prints the resolved configuration, including the defaults Compose fills in (required: true, the default network).

Conditions

conditionWaits until
service_startedthe container has started (default, same as the short syntax)
service_healthythe healthcheck of the dependency reports healthy
service_completed_successfullythe dependency ran to completion with exit code 0 (migrations, seed jobs)
  • restart: true restarts the dependent service when the dependency is restarted by a Compose operation.
  • required: false lets 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