Sub topic 10
Inspecting Health and Using up --wait
This chapter assumes you can write a healthcheck and use depends_on with condition: service_healthy. Here you will check health from the command line and make docker compose up wait for it.
Why inspect health
A depends_on condition only controls start order. It does not tell you why a service is stuck. Two commands show the current state: docker compose ps and docker inspect. The --wait flag makes up block until the stack is usable, which helps in scripts and CI.
The commands
docker compose up -d --wait
docker compose ps
docker inspect --format '{{.State.Health.Status}}' "$(docker compose ps -q db)"Each line does one job:
docker compose up -d --waitstarts the services in the background. It returns once the services are running, or healthy if they define a healthcheck. If a service becomes unhealthy or exits, the command fails with a non-zero exit code.docker compose pslists the containers. TheSTATUScolumn showsUp 12 seconds (healthy),(health: starting), or(unhealthy)for services with a healthcheck.docker inspectreads the health status of one container.docker compose ps -q dbprints the container ID of thedbservice. The format string{{.State.Health.Status}}returnsstarting,healthy, orunhealthy.
If a service has no healthcheck, .State.Health does not exist and docker inspect prints an error. Add a healthcheck first.
How up --wait behaves
With --wait, Compose treats a service as ready in one of two ways:
- If the service has a healthcheck, it must report
healthy. - If it has none, it must be in the
runningstate.
--wait implies -d, so the containers run in the background. The flag also covers one-shot services. A service that exits with code 0 after finishing, such as a migration job, does not make the command fail.
Reading the full health record
The status alone often hides the cause. To see the recent check results, print the whole health object:
docker inspect --format '{{json .State.Health}}' "$(docker compose ps -q db)"The output includes Status, FailingStreak, and a Log list. Each log entry holds the start and end time, the exit code, and the output of the test command. Docker keeps the last five entries. A pg_isready failure shows its message here, for example no response.
Using it in a script
Because --wait returns a meaningful exit code, you can chain it with other commands:
docker compose up -d --wait && ./run-integration-tests.shThe tests run only after every service is ready. If a service never becomes healthy, up fails and the tests do not start.
Common mistakes
- Running
docker inspecton the service name. It needs a container name or ID, so usedocker compose ps -q service. - Reading
pstoo early. A status ofhealth: startingis normal duringstart_period. - Expecting
--waitto check applications with no healthcheck. It only confirms the container is running.
Exercise
You run docker compose up -d --wait and it exits with an error, but docker compose ps shows the db container as Up. Which command shows why the healthcheck is failing?
Show answer
Run docker inspect --format '{{json .State.Health}}' "$(docker compose ps -q db)". The Log entries show the exit code and output of each recent healthcheck run, and FailingStreak shows how many checks in a row have failed.