Docker Compose check · low

depends_on on a service with no healthcheck

depends_on alone makes Compose start a service after its dependency's container has started, not after it is ready to accept connections. Without a healthcheck on the dependency, Compose has no way to know when it is ready. The explainer flags, as low, any service that others depend on but that has no healthcheck. Add one and use condition: service_healthy.

$ explain docker-compose.yml
! cache has no healthcheck low
line 9
fix: see below
$ 

What the free Docker Compose explainer reports on the risky example below.

Why it matters

A database container counts as started as soon as its process launches, often seconds before it accepts connections. Dependents that connect right away fail, and depending on the restart policy they crash-loop or stay down.

Teams often work around this with sleeps or retry scripts in the entrypoint, which hide the problem rather than describe it.

A healthcheck also gives `docker compose ps` and monitoring a real readiness signal, not just whether the process exists.

Risky and safer docker-compose.yml

Riskydocker-compose.yml
services:
  api:
    image: example/api:2.3.0
    depends_on:
      - cache
    restart: unless-stopped
    mem_limit: 512m

  cache:
    image: redis:7.4.1
    restart: unless-stopped
    mem_limit: 512m
Saferdocker-compose.yml
services:
  api:
    image: example/api:2.3.0
    depends_on:
      cache:
        condition: service_healthy
    restart: unless-stopped
    mem_limit: 512m

  cache:
    image: redis:7.4.1
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
    restart: unless-stopped
    mem_limit: 512m

How to fix it

  • Add a healthcheck to the dependency with a command that succeeds only when it is ready, such as pg_isready for Postgres or redis-cli ping for Redis.
  • Switch the dependent's depends_on to the long form with condition: service_healthy.
  • Keep connection retries in the application too: a healthcheck covers start-up, not a dependency that restarts later.

When it's acceptable

  • Dependencies that are ready the moment their process starts, or dependents that already retry their connections gracefully.

Check your own docker-compose.yml

The risky example is loaded below. Paste your own file to check it for this and every other Docker Compose rule. It runs in your browser; nothing is uploaded.

docker-compose.yml

Parsed in your browser. Nothing is uploaded.

Explanation

2 services0 networks0 volumes1 low

Top level

apiexample/api:2.3.0

cacheredis:7.4.1

Also worth a look

Other services depend on it, but Compose can only wait for it to start, not for it to be ready.

Safer: Add a healthcheck and make dependents use condition: service_healthy.

Questions

Does depends_on wait for the database to be ready?

Not by default. It waits for the container to start. To wait for readiness, give the database a healthcheck and use condition: service_healthy.

How do I make docker-compose wait until Postgres is ready?

Add a healthcheck to the Postgres service that runs pg_isready, then set depends_on with condition: service_healthy on the services that use it.

Does the explainer require a healthcheck on every service?

No. It only flags services that another service lists in depends_on, because that is where a missing readiness signal causes start-up failures.