The Up status means the process is alive. Here: how to find out it also answers.
The healthcheck key sets a command Docker runs inside the container on a schedule. Exit code 0 means the check passed; several failures in a row make the container unhealthy.
docker compose ps shows the result: Up 7 seconds (healthy) or Up 15 seconds (unhealthy). localhost in the check is the container itself. Docker does not restart an unhealthy container: it keeps running with the mark.
With a check in place, depends_on can wait for a neighbour's health — condition: service_healthy under the service name. Then up -d starts the dependent service only after the neighbour is Healthy. If the neighbour never becomes healthy, up -d prints dependency failed to start: container … is unhealthy and exits with code 1.
api's healthcheck failed three times in a row, and ps shows (unhealthy). What will Docker do with the container?
-
A stopped container would be
Exited. An unhealthy one staysUp, marked(unhealthy). -
The restart policy fires when the process exits. Here the process is alive:
psshowsUp … (unhealthy), and the restart count does not grow. -
docker compose up -dre-creates containers, and only after their description changes. Docker itself just marks an unhealthy container.
The healthcheck only sets a mark: the process is alive, and the container keeps running as Up … (unhealthy). ps and depends_on with condition: service_healthy see that mark.
Key points
-
healthcheckis a command inside the container;psshows the result as(healthy)or(unhealthy). - An unhealthy container keeps running: Docker only marks it.
-
depends_onwithcondition: service_healthystarts a service after its neighbour is healthy.