The usual assumption is that a container stuck at "health: starting" is hung, broken or waiting for something that will never arrive. Usually it is none of those. Docker is doing exactly what the configuration told it to do: waiting for one passing probe before it calls the container healthy, and refusing to count failures yet. The state can last seconds, minutes or the entire start period, and the timing rules that decide which are rarely read until something blocks a deploy.
This teardown starts with the state machine, because once it is clear, most "never healthy" cases reduce to one of six causes. Each fix below works for a specific reason, and knowing the reason makes the next incident shorter.
How the healthcheck state machine decides between starting, healthy and unhealthy
A container with a healthcheck has three possible health states. It begins in starting the moment the container starts. It moves to healthy after the first probe that exits with code 0. It moves to unhealthy after a number of consecutive failed probes equal to the retries setting. Exit code 1 means failure; code 2 is reserved and should not be used.
The start period changes the counting. During the start period, failed probes do not count toward retries, so the container stays in starting no matter how many probes fail. A successful probe during that period still marks it healthy immediately. After the start period ends, failures begin to count, and the container needs retries consecutive failures to become unhealthy.
Timing matters as much as counting. The first probe does not run at container start: it runs one interval later. Each subsequent probe runs one interval after the previous probe finished. A probe that takes longer than the timeout is killed and counted as a failure. Docker Engine 25.0 added a start interval, which lets you probe more often during the start period than afterwards.
| Setting | Default | Effect on time spent in starting |
|---|---|---|
| interval | 30s | First probe waits this long; long intervals delay the first success |
| timeout | 30s | A hanging probe holds the slot this long before counting as failed |
| start period | 0s | Failures ignored for this long, so starting can last at least this long |
| retries | 3 | Consecutive failures needed after the start period to reach unhealthy |
| start interval | 5s | Engine 25.0 and later: probe spacing during the start period |
From those rules, an upper bound for how long a failing container can remain in starting before it turns unhealthy follows directly:
max time in starting ~ start_period + retries x (interval + timeout)
example: start_period 120s, retries 5, interval 30s, timeout 10s
= 120 + 5 x (30 + 10)
= 120 + 200
= 320 s, a little over 5 minutes
The formula is an approximation, since probe spacing depends on when each probe actually finishes, but it is close enough to explain most incidents. A team that set a 10 minute start period "to be safe" for a slow Java service will watch a broken container sit in starting for over 10 minutes. Nothing is stuck. The configuration is simply generous.
So "stuck in starting" really means one of two things: no probe has succeeded yet and the counting window is still open, or the probes are not running at all. The six causes below fall into one of those two buckets.
The six causes behind most containers that never turn healthy
The first cause is a probe binary that does not exist in the image. Slim and distroless images often ship without curl or wget. The probe exits with code 127, command not found, every time. Within the start period that looks like a container stuck in starting; afterwards it becomes unhealthy.
The fix works because it gives the probe something to run: install a small client, use a static health binary compiled into your app, or call the runtime directly, for example a short Python or Node one liner if the interpreter is already in the image.
The second cause is the exec form written incorrectly in Compose. In a Compose file, test: ["CMD", "curl -f http://localhost:8080/health"] asks Docker to execute a program literally named "curl -f http://localhost:8080/health", which does not exist. The exec form needs every argument as a separate item. Alternatively, use CMD-SHELL with a single string, which runs through /bin/sh. The fix works because Docker does not split strings in exec form; only the shell does.
The third cause is the probe talking to the wrong address. Inside the container, localhost is the container itself, which is usually right. But on Alpine based images, some clients resolve localhost to the IPv6 address ::1 first. If the application listens only on 0.0.0.0 over IPv4, the connection is refused. Using 127.0.0.1 explicitly removes the ambiguity. A related variant is a probe hitting port 80 while the app listens on 8080 because the port was changed in an environment variable that the probe does not read.
The fourth cause is variable interpolation. Compose interpolates $VAR in the Compose file before the container ever sees it. A probe like curl -f http://127.0.0.1:$PORT/health is evaluated on the host at parse time, where PORT may be empty. Writing $$PORT passes a literal dollar sign through to the container shell, so the variable is resolved inside the container where it is actually set.
The fifth cause is timing that cannot succeed. A health endpoint that waits for every downstream dependency, such as a database, a message broker and a third party API, can take longer than the timeout on a cold start. Every probe is killed at the timeout and counted as failed, or ignored during the start period. Make the health endpoint check only what the container itself controls, and keep dependency checks for a separate readiness signal or for the orchestrator.
The sixth cause is a runtime that never runs probes. Podman, for example, schedules healthchecks through systemd timers. In environments without a working systemd, such as some minimal virtual machines or containers inside containers, the timers are never created and the status can sit at starting indefinitely. Running the check manually with the runtime's own healthcheck command confirms whether the probe itself passes.
To tell the buckets apart, read the probe log. docker inspect container_name | jq '.[0].State.Health' returns the current status, the failing streak and the most recent probe results with exit code and output. An empty log means probes are not running. A log full of exit code 127 points to cause one. Output such as "connection refused" points to cause three.
Mini-case: a Compose stack that blocked every deploy for 11 minutes
This case is illustrative, assembled from a common pattern rather than a single real team.
A platform team in Porto runs a Compose stack with an API service, a worker and PostgreSQL. The API uses depends_on with condition: service_healthy on the database, and the deploy script calls docker compose up --wait. After a base image update, deploys start hanging. The API container shows "health: starting" for about 11 minutes, then turns unhealthy and the deploy fails.
The healthcheck in the Compose file reads test: ["CMD-SHELL", "wget -qO- http://localhost:$APP_PORT/health || exit 1"], with interval 60s, timeout 30s, retries 5 and start period 300s. Plugging these into the formula gives 300 plus 5 times 90, which is 750 seconds, or 12.5 minutes as an upper bound. The observed 11 minutes fits.
The probe log shows "wget: can't connect to remote host: Connection refused" on every attempt. Two problems are stacked. First, $APP_PORT is interpolated by Compose on the host, where the variable is unset, so the URL becomes http://localhost:/health. Second, the new base image is Alpine, where wget tries ::1 first, while the API binds IPv4 only.
The team changes the probe to use $$APP_PORT and 127.0.0.1, cuts the interval to 10s, the timeout to 5s and the start period to 60s, and adds a start interval of 2s now that their engine supports it. The API reaches healthy in about 14 seconds. More importantly, a genuinely broken API now fails visibly in 60 plus 5 times 15, roughly 135 seconds, instead of blocking the pipeline for 12 minutes.
The broader lesson is that the generous settings had hidden the real bug for a long time. A 5 minute start period meant nobody saw the probe fail until the base image change made the app slower to bind.
There is a reasonable argument against healthchecks in some setups. If the container runs under an orchestrator with its own probes, or under a process supervisor that already restarts a dead service, the image healthcheck adds a second, slightly different opinion about whether the app is alive. Two opinions can disagree, and on a busy host the extra process every few seconds is not free.
That argument wins when nothing consumes the status: no depends_on condition, no --wait, no Swarm, no monitoring that alerts on unhealthy. In that case a probe is decoration, and removing it is cleaner than tuning it. Once anything gates on the result, though, the probe is part of the deploy path and deserves the same care as the code it checks.
A fix checklist that works in the order problems usually appear
Work through these in order; each one is fast and eliminates a cause before you move to the next.
- Read the probe log with docker inspect before changing anything. Note the exit code and output of the last few probes.
- If the log is empty after more than one interval, check whether probes run at all: confirm the runtime, whether a healthcheck is defined in the final image, and whether it was disabled with
test: ["NONE"]ordisable: true. - Run the exact probe command by hand with docker exec. If it fails there, the problem is the command or the app, not Docker's scheduling.
- Confirm the binary exists in the image: curl, wget or whatever the probe calls.
- In Compose, check the exec form: one array element per argument, or switch to CMD-SHELL with a single string.
- Replace localhost with 127.0.0.1, and confirm the port matches what the app actually binds.
- Escape container side variables with a double dollar sign in Compose files.
- Time the health endpoint on a cold start and set timeout comfortably above it, for example 2 to 3 times the observed duration.
- Shorten interval and start period so failures surface in under 2 to 3 minutes, using a start interval if your engine is 25.0 or newer.
- Keep the health endpoint local to the container. Dependency checks belong in readiness logic or startup ordering, not in a probe that runs every few seconds forever.
- Recalculate the worst case with the formula and confirm it fits inside your deploy timeout, including the timeout you pass to
docker compose up --wait.
Restarts, Kubernetes and the cost of probing
Will Docker restart a container that becomes unhealthy?
Not on a standalone engine. Restart policies react to the main process exiting, not to health status. Docker Swarm replaces unhealthy service tasks, and on a single host people often add a small watchdog container that restarts anything marked unhealthy.
Does Kubernetes use the HEALTHCHECK from my Dockerfile?
No. Kubernetes ignores the image HEALTHCHECK instruction and relies on its own liveness, readiness and startup probes defined in the pod spec, so an image can be healthy in Compose and still have no checks at all in a cluster.
Where can I see the health status without docker inspect?
The STATUS column of docker ps shows it in brackets, for example Up 3 minutes (health: starting). Docker also emits a health_status event each time the state changes, which docker events can stream.
Do frequent probes cost performance?
Each probe starts a new process inside the container. A shell plus curl every 5 seconds is negligible on most services, but a probe that runs a heavy script, opens a database connection or warms a JVM can add measurable load on small hosts.
Every fix above works for one of the two reasons the state machine gives. Either it lets a probe actually succeed (a binary that exists, a correct exec form, the right address and port, a local endpoint), or it makes failures count sooner (a shorter start period and interval, a start interval on Engine 25.0 and later).
A change that does neither will not move a container out of starting, however often it is suggested. Before the next deploy, run the worst-case formula against your own settings. If the result is longer than the timeout you give docker compose up --wait, the probe will block the pipeline before it ever reports a failure.