December 5, 2026

Docker Compose Healthcheck and depends_on: Making Services Wait Until They’re Ready

Two shipping containers on a dock, one lit and open, one dark

A Compose file says the app depends_on the database. The app starts, tries to connect, gets “connection refused,” and exits, while the database is still booting a few seconds behind it. depends_on did exactly what it promises: it started the database first. It never promised to wait until the database was ready. The fix is a healthcheck plus one extra line, and getting the healthcheck itself right turned out to have its own trap. Everything below was run with Docker Engine and the Docker Compose plugin, using small BusyBox containers: a fake database that takes eight seconds to start answering, and two clients.

Video: WittCode on YouTube

The race depends_on doesn’t solve

The test setup

The “database” container printed a boot message, slept eight seconds, created a /tmp/ready file, then started answering on port 5432. A client called naive used the short form:

naive:
  image: localbase:1
  command: ["sh", "-c", "... nc -w 2 db 5432 ..."]
  depends_on:
    - db

What happened

naive-1  | naive started 14:32:56
naive-1  | nc: can't connect to remote host (172.20.0.2): Connection refused
naive-1  | naive-db-not-answering

The database container had started, which satisfied depends_on, but nothing inside it was listening yet. The Compose workload specification says so in plain terms: Compose does not wait for dependency services to be “healthy” before starting a dependent service. The short form only controls the order in which containers are started.

Why it works on some machines

A fast laptop with a warm image cache can start the database quickly enough that the race goes unnoticed. A slower CI runner, a first-time image pull or a database replaying its logs after a crash changes the timing, and the same file fails. A setup that works only because of timing is waiting for the day the timing changes.

A runner pushing off starting blocks on a red track

Adding a healthcheck

What a healthcheck is

A healthcheck is a command Docker runs inside the container on a schedule. The Dockerfile reference defines the exit codes: 0 means healthy and ready for use, 1 means unhealthy, and 2 is reserved and shouldn’t be used. Docker records the result and shows the container as starting, healthy or unhealthy.

The first attempt failed for a surprising reason

healthcheck:
  test: ["CMD", "test", "-f", "/tmp/ready"]

The container never became healthy. docker inspect showed why:

"Output":"OCI runtime exec failed: exec failed: unable to start container process: exec: \"test\": executable file not found in $PATH"

With the CMD form, Docker runs the first item as a program file. In this minimal image, test existed only as a shell built-in, not as a file on disk, so there was nothing to execute. The check failed every time, no matter whether the database was ready.

CMD-SHELL fixed it

healthcheck:
  test: ["CMD-SHELL", "test -f /tmp/ready"]

The Compose specification describes CMD-SHELL as running the command string with the container’s default shell, /bin/sh on Linux. Through the shell, the built-in test worked. The same trap catches checks that call curl or wget in images that don’t include them; the healthcheck needs a tool that actually exists inside the container.

Making the app wait

The long form of depends_on

app:
  image: localbase:1
  command: ["sh", "-c", "... nc -w 2 db 5432 ..."]
  depends_on:
    db:
      condition: service_healthy

The Compose specification lists three conditions:

Condition Waits for
service_started The container to start (same as the short form)
service_healthy The dependency’s healthcheck to report healthy
service_completed_successfully The dependency to run to completion and exit successfully

The timeline

Time Event
14:33:29 Database container boots
14:33:32, :34, :36 Healthcheck runs, exits 1 (not ready)
14:33:37 Database creates its ready file
14:33:38 Healthcheck exits 0; Compose reports Healthy
14:33:39 App starts, connects, gets an answer

The app waited about ten seconds without a single retry loop in its own code, and its first connection succeeded.

When the dependency never gets healthy

A second run made the database take 60 seconds to get ready, with only 3 retries allowed. Compose gave up:

Container p22b-db-1 Error dependency db failed to start
Container p22b-app-1 not started: condition "service_healthy" of service "db" not met
dependency failed to start: container p22b-db-1 is unhealthy

The app never started, and the command exited with an error. That is the right failure: a clear message naming the unhealthy dependency is easier to debug than an app that crashes with a connection error.

One-shot jobs: service_completed_successfully

The third condition handles setup tasks such as database migrations, which should run once and finish before the app starts. A test migrate service slept three seconds and exited with status 0, and the app was set to depend on it with condition: service_completed_successfully:

migrate start 14:35:20
migrate done 14:35:23
app-1  | app start 14:35:24

The app started a second after the migration finished. Changing the migration to exit with status 1 stopped everything:

Container p22d-migrate-1 Error service "migrate" didn't complete successfully: exit 1
Container p22d-app-1 not started: condition "service_completed_successfully" of service "migrate" not met

That protects the app from running against a half-migrated database. Chaining the two conditions, a migration that waits for the database to be healthy and an app that waits for the migration to complete, gives a startup order that doesn’t depend on luck at any step.

A close up of a clock with a heartbeat on it

Tuning the timing

The settings and their defaults

The Dockerfile reference lists the defaults: an interval of 30 seconds, a timeout of 30 seconds, a start period of 0 seconds, a start interval of 5 seconds, and 3 retries. With those defaults, a dependent service could wait up to 30 seconds after the database is actually ready, just for the next check. The test used a 2-second interval to keep startup quick.

What start_period does

The same reference explains the start period as initialization time for containers that need it to bootstrap. Failures during that window don’t count toward the retry limit, but if a check succeeds during it, the container is considered started and later failures count normally. A database that takes a minute to start on cold storage needs a start period of at least a minute, or the retries run out while it is still legitimately booting.

Rough budget

The longest a dependency can take before being marked unhealthy is about start_period + interval × retries. With the test’s 3-second start period, 2-second interval and 10 retries, that was roughly 23 seconds. The failing run cut it to 1-second start period and 3 retries, about 7 seconds, which is why a 60-second startup failed.

Writing a good healthcheck

Check readiness, not existence

The ready file in the test is a stand-in. Real checks should ask the service whether it can do its job: a database client command that runs a trivial query, an HTTP request to a health endpoint that checks its own dependencies, or a cache client’s ping. A check that only confirms the process exists says nothing a running container doesn’t already say.

Keep it cheap

The check runs every interval for the life of the container. A query that scans a table or an endpoint that calls three other services adds load forever. A fast, local check is enough.

Make sure the tool is in the image

The CMD failure in the test is common enough to repeat: minimal and distroless images often lack a shell, curl, wget or even test. If the image has a shell, CMD-SHELL with built-ins works. If it has neither, the check needs a small binary added to the image, or the application’s own health command if it ships one.

Pendant LED light glowing near wall

Beyond startup order

Seeing health status

docker compose ps shows each container’s status with its health in parentheses, such as Up 22 seconds (unhealthy). For the reason behind a failing check, docker inspect on the container lists the last few check results with their exit codes and output, which is exactly where the “executable file not found” message in the test turned up.

Healthchecks don’t restart anything by themselves

Plain Docker records a container as unhealthy but does not restart it on that basis alone. Restart policies react to the container exiting, not to its health status. Orchestrators such as Docker Swarm and Kubernetes do act on health, but in Compose on a single machine, an unhealthy container keeps running unless something else intervenes.

Apps should still retry

service_healthy only covers startup. If the database restarts at 3 a.m., the app sees the same connection errors with no Compose dependency logic involved. Connection retries with a short backoff in the application, or a restart policy that brings a crashed app back, cover what the healthcheck can’t.

The two lines that matter

A healthcheck that runs a real readiness command through a tool that exists in the image, and condition: service_healthy on the services that need it. Startup order becomes startup readiness, and the race that only shows up on a slow machine on the worst possible day stops happening.

Sources

  • Compose file reference, workload specification (depends_on and healthcheck), Docker Docs: https://docs.docker.com/reference/compose-file/workload-spec/
  • Dockerfile reference (HEALTHCHECK), Docker Docs: https://docs.docker.com/reference/dockerfile/

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *