Sub topic 12
Troubleshooting unhealthy and failed dependencies
This chapter assumes you can read a compose.yaml, run docker compose up, and already know depends_on conditions and healthcheck options. It covers what to check when a dependency never becomes healthy or a one-shot job fails.
How a failure shows up
When a service uses condition: service_healthy and its dependency never reaches healthy, docker compose up stops with a message like dependency failed to start: container db is unhealthy. The dependent service is never created. If a job fails under service_completed_successfully, the message says the dependency exited with a non-zero code.
Step 1: look at the state
docker compose ps -a
docker compose logs db
docker inspect --format '{{json .State.Health}}' "$(docker compose ps -q db)"ps -a includes exited containers, which matters for migration jobs. The State.Health output holds the status and a log of recent probe runs, each with an exit code and output. That log is usually the fastest route to the cause.
Common causes
| Symptom | Likely cause | Fix |
|---|---|---|
| Probe output says `executable file not found` or exit code 127 | The image does not contain the command, for example `curl` in a slim image | Use a tool the image has, or install it in the Dockerfile |
| Probe says `role "postgres" does not exist` or similar | The probe uses the wrong user or database | Match `pg_isready -U` to the configured user |
| Container is `unhealthy` after a slow first start | Probes fail until the service finishes initializing, and the retries run out | Raise `start_period` or `retries` |
| Probe times out | `timeout` is shorter than the command's run time | Raise `timeout` |
| Dependent service never starts, no error | Dependency is `starting` for a long time | Lower `interval` or check why the service is slow |
| Job exits with code 1 | The migration failed, often because it cannot reach the database or the credentials are wrong | Read `docker compose logs migrate` |
| `up` fails on a job that you expected to be skipped | `service_completed_successfully` requires exit code 0 | Fix the job, or make it tolerate a re-run |
Test the probe by hand
A healthcheck is an ordinary command run inside the container. Run it yourself and read its exit code:
docker compose exec db pg_isready -U postgres
echo $?Exit code 0 means healthy and 1 means unhealthy. Other codes are reserved. If exec itself fails with "not found," the probe command is missing from the image, and the healthcheck can never pass.
Check the job's exit code
docker compose ps -a migrate
docker inspect --format '{{.State.ExitCode}}' "$(docker compose ps -aq migrate)"A non-zero code means web will not start. Fix the cause, then run docker compose up -d again. Compose recreates only what is needed.
Reproduce from a clean state
A volume can hold data from an earlier run, for example a database initialized with a different password. The healthcheck may then fail because of that old state and not because of your current file. To start fresh, remove the volumes:
docker compose down -v
docker compose up -d --waitThe -v flag deletes named volumes and their data. Use it only when the data can be lost.
A healthy dependency can still fail the app
If the dependency is healthy but the application still cannot connect, the healthcheck is probably testing too little. For example, pg_isready checks that the server accepts connections, not that your database or schema exists. Make the probe test what the application needs, or add connection retries in the application.
Exercise
A web service uses depends_on: db: condition: service_healthy. docker compose up reports that db is unhealthy, and docker inspect shows the probe output exec: "curl": executable file not found in $PATH. What is wrong, and how do you fix it?
Show answer
The healthcheck runs curl, but the db image does not contain it, so the probe fails on every attempt and the container ends up unhealthy. Change the test to a command the image has (for example pg_isready -U postgres for PostgreSQL) or install curl in the image. Increasing retries or start_period will not help.