Difficulty: Intermediate
What does the HEALTHCHECK instruction in a Dockerfile do, and how does Docker use the result?
HEALTHCHECK tells Docker how to test whether the application inside a running container is actually working, not just whether its process exists. Docker runs the check command on a schedule, and based on its exit code marks the container starting, healthy or unhealthy, which orchestrators like Compose and Swarm then use for startup ordering and replacement.
A container is "Up" as long as PID 1 is alive. A Node.js API stuck in an infinite loop, a Spring Boot app that has lost its database pool, or a server deadlocked on a mutex are all still running. Without a health check, nothing notices. With one, docker ps shows Up 5 minutes (unhealthy) and tools that consume health status can act on it.
HEALTHCHECK [OPTIONS] CMD <command>
| Option | Default | Meaning |
|---|---|---|
--interval | 30s | Time between checks |
--timeout | 30s | A check taking longer counts as failed |
--start-period | 0s | Grace period for slow boots; failures here do not count |
--start-interval | 5s | Check frequency during the start period (Docker Engine 25+) |
--retries | 3 | Consecutive failures before unhealthy |
The command must exit 0 for healthy and 1 for unhealthy (avoid other codes). Only the last HEALTHCHECK in a Dockerfile counts, and HEALTHCHECK NONE disables one inherited from a base image.
docker run: only reports it. An unhealthy container is not restarted automatically; --restart policies react to exits, not health.depends_on with condition: service_healthy makes, say, the API wait until Postgres is genuinely accepting connections.HEALTHCHECK entirely. You define livenessProbe, readinessProbe and startupProbe in the pod spec instead.curl in a slim or distroless image where it is not installed. The check fails forever. Use wget -q --spider, a tiny binary, or a script in your app's own runtime.--start-period for apps like JVM services that take 40 seconds to boot, so they are marked unhealthy before they ever start.Inspect results with docker inspect --format '{{json .State.Health}}' <container>, which shows the last few check outputs and the failing streak.
# Dockerfile (excerpt)
FROM node:22-alpine
WORKDIR /app
COPY . .
RUN npm ci --omit=dev
EXPOSE 3000
# wget ships with busybox in alpine; curl does not
HEALTHCHECK --interval=15s --timeout=3s --start-period=20s --retries=3 \
CMD wget -q --spider http://localhost:3000/healthz || exit 1
CMD ["node", "server.js"]
The `|| exit 1` normalises any failure to exit code 1. The start period gives the app 20 seconds to boot before failures count.
services:
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 10
api:
build: .
depends_on:
db:
condition: service_healthy
Compose starts `api` only after `db` reports healthy, which fixes the classic 'connection refused on first boot' race.
HEALTHCHECK, Health Status, start-period, depends_on service_healthy, Liveness Probe