Docker Healthcheck (2026): What It Really Does, Measured in 14 Experiments

Docker Healthcheck (2026): What It Really Does, Measured in 14 Experiments

A Docker healthcheck is a small command that Docker runs inside the container at regular intervals to find out whether the application in there is still doing its job. If the command returns exit code 0, the container counts as healthy; after a few failures it becomes unhealthy. That sounds simple, and it is. Things only get complicated when you ask what happens next. That is exactly where most tutorials get it wrong.

On 28 November 2026 we ran 14 experiments with throwaway containers on one of our Linux servers (Docker 29.9.3, Docker Compose v5.7.1, nginx:alpine with nginx 1.31.8). Each container lived for a few seconds, got a name starting with hc-, and was deleted afterwards. One result up front, because it matters most:

A container with --restart=always whose healthcheck flipped to unhealthy was not restarted a single time during 60 seconds of observation. RestartCount stayed at 0, the state stayed running/unhealthy. When we instead killed the main process with kill 1, the container was back three seconds later, RestartCount 1.

A healthcheck reports. It does not repair anything. Without Docker Swarm, Kubernetes, a deploy tool like Coolify, or a watcher of your own, a sick container simply stays sick, just with a new label. How to deal with that, and how to write a healthcheck that is actually useful, follows below. If you are just getting started: what containers and images are in the first place is covered in What is Docker?.

The video (January 2026) explains how healthchecks and restart policies relate. Our measurements below show exactly where the line between the two runs.

What is a Docker healthcheck?

Without a healthcheck, Docker knows exactly one thing about your container: is the main process (PID 1) running or not? That is all Docker can see. A web server that is running but answers every request with a 500 error because its database connection is gone looks perfectly fine to Docker. docker ps happily shows Up 3 days.

A healthcheck closes that gap. You give Docker a command, for example an HTTP request to /health, and Docker runs it regularly. The result becomes a second state, independent of the first:

StateMeaning
startingContainer is running, but no check has produced a result yet (or the start period is still running)
healthyThe last check succeeded (exit code 0)
unhealthySeveral checks in a row failed, as many as retries

docker ps shows this in brackets after the status: Up 2 seconds (healthy). No brackets means no healthcheck. On our server, by the way, only 1 of 8 running containers had one. That is typical: most official images do not ship with a healthcheck.

Three identical containers in a row, the first with an amber, the second with a green and the third with a red status light, connected by a glowing line

Docker only cares about the command’s exit code:

  • 0 = healthy
  • 1 = unhealthy
  • 2 = reserved, “do not use”, according to the documentation

We wanted to know what the reserved code 2 actually does. Result: it is treated like 1. Our container with --health-cmd='exit 2' was unhealthy within seconds. A command that does not exist at all (gibtsnicht, German for “doesn’t exist”) returns exit code 127 and also ends in unhealthy. So a typo in your healthcheck looks exactly like a broken application. That matters for debugging, more on that later.

HEALTHCHECK in the Dockerfile vs. healthcheck in Docker Compose

There are two places to define a healthcheck, and both end up doing the same thing.

In the Dockerfile, the healthcheck becomes part of the image:

FROM nginx:alpine
RUN echo ok > /usr/share/nginx/html/health
HEALTHCHECK --interval=2s --timeout=1s --start-period=0s --retries=3 \
  CMD wget -qO- http://127.0.0.1/health || exit 1

Anyone who starts this image gets the healthcheck automatically. We verified that with docker inspect on a freshly created, never started container. Config.Healthcheck contained:

{"Test":["CMD-SHELL","wget -qO- http://127.0.0.1/health || exit 1"],
 "Interval":2000000000,"Timeout":1000000000,"Retries":3}

Two things stand out. First, Docker turned our CMD wget … into CMD-SHELL, because we used the shell form. Second, the durations are stored in nanoseconds. If you parse that JSON in a script, you need to know that.

In Docker Compose, the healthcheck lives in compose.yaml under the service:

services:
  web:
    image: my-app:1
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1/health"]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 20s
      start_interval: 1s

A healthcheck in Compose overrides the one from the image. With healthcheck: disable: true (or docker run --no-healthcheck) you switch off an inherited one. We tested this: with --no-healthcheck the container had no health state at all, docker ps only showed Up 1 second, no brackets.

Which place is better? Our rule of thumb:

  • Dockerfile, if you build the image yourself and the check belongs to the application (for example the /health endpoint of your own API). Then everyone using the image gets the check, even without Compose.
  • Compose, if you use someone else’s image (Postgres, Redis, Nginx) or if the check depends on your environment, such as a port or password.

How healthchecks work together with depends_on, so that an app only starts once its database is ready, we measured in detail in our Docker Compose example. Here we focus on the healthcheck itself.

CMD or CMD-SHELL? A trap with variables

Compose accepts two forms for test:

test: ["CMD", "wget", "-qO-", "http://127.0.0.1/health"]   # exec form, no shell
test: ["CMD-SHELL", "wget -qO- http://127.0.0.1/health || exit 1"]  # with /bin/sh
test: "wget -qO- http://127.0.0.1/health || exit 1"         # short form = CMD-SHELL

The exec form starts the program directly, without a shell. It is slightly faster and works even in images with no shell at all. The catch: environment variables are not expanded. We started a container with -e P=/health and tested ["CMD","wget","-qO-","http://127.0.0.1$P"] once and the shell form wget -qO- http://127.0.0.1$P once. The exec form ended up unhealthy, because wget literally requested http://127.0.0.1$P. The shell form was healthy after one second.

Careful, Compose adds a second layer: it replaces $P while reading the file, using values from your shell or .env, not from the container. If you need a variable from inside the container, write $$P and use CMD-SHELL. We explain the two layers of variables in Compose in the Compose article.

The five settings: interval, timeout, retries, start_period, start_interval

Almost every tutorial lists the options. We wanted to see how they behave in time. Our test subject was an nginx:alpine image that could delay the web server start by a few seconds via a DELAY environment variable, so we could simulate a slow-starting application.

OptionDefaultWhat it does
interval30sTime between two checks
timeout30sHow long a single check may take
retries3Consecutive failures until unhealthy
start_period0sGrace period after start: failures do not count
start_interval5sTime between checks during the grace period

Experiment 1: When does a healthy container become healthy?

With interval: 2s and an nginx that starts immediately, the container was healthy after 2.2 seconds. So the first check does not run at startup; it runs after the first interval. With the defaults (30 seconds) that means: a container without start_period stays starting for at least half a minute, no matter how fast the application is. Anything waiting for healthy, such as depends_on or docker compose up --wait, waits that long too.

Experiment 2: Slow start without a grace period

Now nginx took 15 seconds to start, the healthcheck had no start_period, interval: 2s, retries: 3. Timeline:

 0.0 s  running/starting
 6.1 s  running/unhealthy
16.4 s  running/healthy

After three failed checks (at 2, 4 and 6 seconds) the container was unhealthy, even though nothing was wrong with it; it simply was not ready yet. As soon as nginx was up, it flipped back to healthy on its own, and FailingStreak went back to 0. So unhealthy is not a final state. But any tool that looked at the status during those ten seconds would have raised a false alarm. docker compose up --wait aborts in that situation, more on that in a moment.

Experiment 3: The same situation with start_period: 20s

 0.0 s  running/starting
15.3 s  running/healthy

Not a single unhealthy. Failures inside the start_period do not count. But as soon as one check succeeds during the grace period, the grace period ends and the container counts as healthy immediately. You do not have to wait out the full period. A generous start_period costs nothing as long as the application is ready earlier.

Experiment 4: start_interval, the underrated option

The problem: a long interval of 30 seconds makes sense in production because it saves resources. During startup, though, you want to know quickly when the container is ready. That is what start_interval is for, available since Docker Engine 25. We used an application that was ready after 6 seconds, with interval: 30s and start_period: 20s:

SettingTime to healthy
start_interval: 1s6.4 s
without start_interval (default 5 s)10.3 s
for comparison: without start_periodfirst check only after 30 s

Two takeaways. First, start_interval brings the wait down almost exactly to the real startup time. Second, and none of the tutorials we read said this: even without setting it, Docker 29 checks every 5 seconds during the start period, not every 30. Our container was checked at 5 seconds (not ready yet) and at 10 seconds (ready), hence the 10.3. That default only kicks in if a start_period is set.

Experiment 5: What happens on a timeout?

We built a healthcheck that simply hangs (sleep 5), with timeout: 1s, interval: 2s, retries: 2. After 6.2 seconds the container was unhealthy. Every entry in the health log read:

-1 Health check exceeded timeout (1s)

So exit code -1 means: not failed, but cut off. That is gold when debugging, because it separates a slow application from a broken one. The default timeout of 30 seconds is far too generous for an HTTP check against localhost; a hanging check would block for 30 seconds before counting as failed. We use 3 to 5 seconds.

The big misconception: does Docker restart an unhealthy container?

Now for the most important question: how do healthchecks and restart relate? Many guides give the impression that a container with restart: always and a healthcheck heals itself when something goes wrong. We checked.

Setup: a container with --restart=always and our healthcheck on /health. After four seconds, once it was healthy, we deleted the /health file inside the container. nginx kept running but now returned a 404 for /health. Then we polled the container every 0.2 seconds for 60 seconds.

Result:

 0.0 s  running/healthy    RestartCount=0
 4.1 s  running/unhealthy  RestartCount=0
 ... no change for 60 seconds ...

No restart. The container stayed unhealthy for a full minute with RestartCount 0, and it would have stayed that way forever without our intervention.

Control experiment: same container, but this time we ended the main process (docker exec … kill 1). Three seconds later: RestartCount=1, state running/healthy. So the restart policy works, but it reacts only to a process that exited, never to the health status.

A container that looks intact from the outside with its lights on, but is empty and dusty inside with a disconnected cable

One more detail worth knowing: while the container was unhealthy, we requested it via a published port (127.0.0.1:18471). The front page answered with HTTP 200. Docker keeps routing traffic to an unhealthy container. The status has no effect on networking. A reverse proxy like Nginx or Caddy does not know about it either. (Traefik can evaluate Docker’s health status, but only if you configure it to.)

So who reacts to unhealthy?

Not the Docker daemon itself. But several tools read the status and act on it:

  • Docker Swarm replaces tasks whose containers become unhealthy. If you run Swarm, you really do get self-healing.
  • Docker Compose uses the status at startup: depends_on with condition: service_healthy and docker compose up --wait. While running, Compose does nothing with it.
  • Deploy tools like Coolify or Dokploy use it during updates: if the new container never becomes healthy, the old one stays. In our Coolify review that is exactly what caught a broken release. Without a healthcheck, Coolify reported “finished” while the app was stuck in a restart loop. We measured both tools side by side in Coolify vs Dokploy.
  • A watcher of your own. That can be a small script listening to Docker events, or a ready-made project like willfarrell/autoheal, which restarts containers carrying a certain label as soon as they turn unhealthy. We did not test autoheal. It needs access to the Docker socket, and that is a security risk: whoever holds the socket effectively has root on the host.
  • Monitoring that polls the status and alerts you. For many small setups that is the most honest solution: a human takes a look instead of a script restarting blindly. How we monitor our own servers is in Server Monitoring.

A minimal watcher using Docker events

Docker emits every change of health status as an event. We captured it with docker events:

docker events --filter event=health_status --format '{{.Actor.Attributes.name}} {{.Action}}'

The output looks like hc-p health_status: healthy. Note: the field is .Action, not .Status. Our first attempt with {{.Status}} failed with can't evaluate field Status in type *events.Message. Older guides still use .Status.

From that you can build a simple watcher:

#!/bin/sh
# Restarts containers that turn unhealthy. Only for containers labelled autoheal=true.
docker events --filter event=health_status --filter label=autoheal=true \
  --format '{{.Actor.Attributes.name}} {{.Action}}' |
while read -r name action; do
  case "$action" in
    *unhealthy) echo "$(date -Is) $name unhealthy, restarting"; docker restart "$name" ;;
  esac
done

Run as a systemd service (how to set one up is in Create a systemd service), this script replaces the self-healing you might have expected from restart: always. Two warnings: a container that turns unhealthy because of a real third-party problem (database gone) will be restarted endlessly without fixing anything. And the watcher runs on the host, so no container needs the socket. That is its main advantage over autoheal.

The video (August 2026) shows one way to get automatic restarts. Compare it with our result above: the restart policy alone is not enough.

Healthchecks and Docker Compose: depends_on and --wait

At startup, the healthcheck in Compose is genuinely useful. We tested two stacks.

Stack 1: a service web with a working healthcheck (start_interval: 1s), a service app that waits for it via depends_on: web: condition: service_healthy, and a service noh with healthcheck: disable: true. Command:

docker compose up -d --wait

After 2.6 seconds we got exit code 0, all three services were running, web was healthy. Interesting detail: the --wait output also printed “Healthy” for noh, even though it had no healthcheck at all. With --wait, Compose treats services without a healthcheck as “running = done”. If you rely on that output, keep it in mind: “Healthy” there does not mean a check ran.

Stack 2: the same layout, but the web healthcheck requested a page that does not exist. Result after 2.8 seconds:

Container hctest2-web-1 Error dependency web failed to start
dependency failed to start: container hctest2-web-1 is unhealthy

Exit code 1; the app service stayed in state Created and was never started. web kept running, marked (unhealthy). So Compose does not clean up, it only holds back the dependent services.

For deploy scripts and CI this is very handy: docker compose up -d --wait && echo "Deploy ok" fails when a service does not become healthy, instead of giving a broken release the green light. But remember experiment 2. Without start_period, a slow-starting app can become unhealthy temporarily, and --wait aborts, even though it would have been healthy a second later. With --wait, always set a start_period longer than your slowest realistic startup.

Several stacked containers waiting behind a glowing gate that only opens once the first container shows a green signal

Writing a good healthcheck

A healthcheck can go wrong in three ways: it checks too little, it checks too much, or it does not work in the image at all.

Problem 1: the image has no curl

Most examples online use curl -f. We looked at what common images actually contain:

Imagecurlwget
nginx:alpineyesyes
alpine:latestnoyes (BusyBox)
node:22-bookworm-slimnono

Of all images, the slim Node image has neither curl nor wget. A curl healthcheck runs into exit code 127 there and reports unhealthy, even though the app is running perfectly. Distroless images do not even have a shell. The smaller the image, the more likely this gets. If you slimmed your image down following our guide to reducing Docker image size, double-check that your healthcheck still finds a tool.

The fix: use the language that is in the image anyway. For Node:

healthcheck:
  test: ["CMD", "node", "-e", "require('http').get('http://127.0.0.1:3000/health', r => process.exit(r.statusCode === 200 ? 0 : 1)).on('error', () => process.exit(1))"]
  interval: 30s
  timeout: 3s
  retries: 3
  start_period: 30s
  start_interval: 1s

In our test the Node container was healthy within seconds, with the whole container using 10.5 MiB of memory. Python works the same way with python -c "import urllib.request,sys; …"; for Go, it is best to build a dedicated -health flag into the binary.

You can spot the “command not found” case immediately in the health log:

docker inspect --format '{{json .State.Health}}' my-container | jq

It lists the exit code and output of every attempt, for example 127 /bin/sh: gibtsnicht: not found. Docker keeps the last five attempts. In our test with one check per second, FailingStreak was at 11 after 12 seconds, but the log only had 5 entries.

A side effect we noticed: the check’s entire output lands in the log, including curl’s progress meter and the full HTML of the page. That bloats docker inspect. With curl -fsS -o /dev/null or wget -q --spider the log stays clean.

Problem 2: the check checks too little

test: ["CMD", "true"] is always healthy and therefore worthless. Almost as worthless is a check that only looks at whether a port is open. The process can open the port and still answer every request with an error. A good check requests an endpoint the application answers itself and expects a clear success code.

Problem 3: the check checks too much

The opposite is just as dangerous. A /health endpoint that queries the database, Redis, an third-party payment provider and three other APIs turns unhealthy as soon as any of them hiccups. If a watcher or Swarm then reacts with restarts, half your infrastructure restarts because someone else’s service was slow. That fixes nothing; it only makes the outage bigger.

Our rule: the healthcheck answers the question “would restarting this container help?” Process hung, memory full, web server dead: yes. Database on another server gone: no, that belongs in monitoring, not in the healthcheck.

Kubernetes separates this cleanly into “liveness” (is the process alive?) and “readiness” (may it receive traffic?). Docker only has one healthcheck, so you have to choose. On a single server, the liveness variant is usually the better choice.

Problem 4: the check is too expensive

A check every 2 seconds is handy for testing but unnecessary in production. Every check starts a new process inside the container. For a Node check that means spinning up a new Node instance every few seconds, for every container. We recommend interval: 30s in production and start_interval: 1s for a fast start. That way you get both.

Healthchecks for common services

These are checks we use ourselves or that the official images suggest:

# PostgreSQL
healthcheck:
  test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
  interval: 10s
  timeout: 3s
  retries: 5
  start_period: 30s
  start_interval: 1s

# MySQL / MariaDB (official MariaDB image)
healthcheck:
  test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
  interval: 10s
  timeout: 5s
  retries: 5
  start_period: 60s

# Redis
healthcheck:
  test: ["CMD", "redis-cli", "ping"]
  interval: 10s
  timeout: 3s
  retries: 3

# Nginx / static site
healthcheck:
  test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1/"]
  interval: 30s
  timeout: 3s
  retries: 3

Note the double $$ in the Postgres check: it makes the variable expand inside the container, not when the Compose file is read. For databases, a working backup is worth at least as much as a healthcheck; see MySQL backup and PostgreSQL backup.

A control room with monitors full of pulse curves watching a row of small containers, with a robotic arm ready to replace one of them

Common mistakes and how to spot them

SymptomLikely causeCheck with
Container stays starting for 30 sNo start_period/start_interval, default interval 30 sdocker inspect → Config.Healthcheck
Instantly unhealthy, app worksTool missing in image (exit 127)Health log, docker exec … which curl
unhealthy with exit code -1Timeout exceededHealth log: “exceeded timeout”
unhealthy, but no restartRestart policy ignores healthexpected, you need a watcher
compose up --wait aborts on deploystart_period shorter than real startupmeasure your app’s startup time
Check cannot find variableexec form without shell, or $ instead of $$ in ComposeCMD-SHELL + $$VAR
Unhealthy container still gets trafficDocker and most proxies ignore the statusproxy configuration

Limits of these measurements

All numbers come from a single server running Docker 29.9.3 and Compose v5.7.1. Older Docker versions (before Engine 25) do not know start_interval and ignore it. Exact timings vary by a few tenths of a second because we polled every 0.2 seconds. We did not test Docker Swarm, Kubernetes, Traefik or autoheal ourselves; statements about them come from their documentation. Our experiments used very short intervals (1 to 2 seconds) so they would finish quickly. We would not configure production that way.

Conclusion: a Docker healthcheck is a thermometer, not a doctor

A Docker healthcheck is quick to set up and almost always worth it. It turns “the process is running” into a real statement about the application, it makes depends_on and docker compose up --wait wait properly, and with deploy tools like Coolify it protects you from broken updates.

What it does not do is repair. Our key number: 60 seconds unhealthy with restart: always, zero restarts. If you want self-healing, you need Swarm, a deploy tool or a watcher of your own, and if you have none of those, you should at least monitor the status.

Our template for our own applications:

healthcheck:
  test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1:3000/health"]
  interval: 30s
  timeout: 3s
  retries: 3
  start_period: 30s
  start_interval: 1s

Plus a /health endpoint that only checks whether a restart would help, and a tool in the image that can actually run the check.

Frequently asked questions

What is a Docker healthcheck?

A command Docker runs inside the container at regular intervals. Exit code 0 means healthy, anything else unhealthy. This produces a state (starting, healthy, unhealthy) shown in addition to the normal running state, for example Up 2 minutes (healthy) in docker ps.

Does Docker restart an unhealthy container automatically?

No, not with a normal restart policy. In our test, a container with --restart=always stayed unhealthy for 60 seconds without a single restart. The restart policy only reacts when the main process exits. Automatic restarts on unhealthy come from Docker Swarm, deploy tools or a watcher of your own.

What is the difference between HEALTHCHECK in the Dockerfile and healthcheck in Compose?

None in effect. In the Dockerfile the check becomes part of the image and applies to everyone who runs it. In Compose it applies only to that stack and overrides a check from the image. We recommend the Dockerfile for your own images and Compose for third-party images.

How long does it take until a container is healthy?

At least one interval, because the first check does not run immediately. With the defaults that is 30 seconds. With start_period and start_interval: 1s, our container was healthy after 6.4 seconds, for an application that was ready after 6 seconds.

What does start_period mean?

A grace period after startup during which failed checks do not count. If a check succeeds during the grace period, the container is healthy immediately. Without it, our slow-starting app became unhealthy after 6 seconds although it simply was not ready yet.

What is start_interval?

The time between checks during the start_period, available since Docker Engine 25. The default is 5 seconds. It lets you check rarely in production (say every 30 seconds) while still detecting quickly when the container is ready at startup.

Why is my container unhealthy although the app is running?

Most often the tool used by the check is missing. node:22-bookworm-slim contains neither curl nor wget, so the check fails with exit code 127. Find the cause with docker inspect --format '{{json .State.Health}}' NAME. Other causes: a timeout that is too short (exit code -1), the wrong port, or a variable that the exec form does not expand.

How do I see the result of a healthcheck?

With docker inspect --format '{{json .State.Health}}' NAME. It shows the current status, the number of consecutive failures (FailingStreak) and the last five attempts with exit code and output. Live changes are shown by docker events --filter event=health_status.

Does an unhealthy container still receive requests?

Yes. In our test an unhealthy container kept answering HTTP 200 on its published port. Docker routes traffic regardless of health status. Only tools that explicitly evaluate the status (Swarm, Traefik with the right settings) react to it.

Should the healthcheck test the database?

Usually not. A healthcheck should answer whether restarting the container would help. If the database on another server is down, restarting the app does not help. Such dependencies belong in monitoring. The check should verify that the application itself still responds.

Can I disable a healthcheck from an third-party image?

Yes, with healthcheck: disable: true in Compose or docker run --no-healthcheck. In our test the container had no health state afterwards. Note: docker compose up --wait still reports such a service as “Healthy” once it is running.

What does exit code -1 mean in the health log?

The check took longer than timeout and was cut off. The log then says Health check exceeded timeout. That separates a slow application from one that reports an error. The default timeout of 30 seconds is usually too long for local HTTP checks; 3 to 5 seconds is enough.