HEALTHCHECK

Difficulty: Intermediate

Question

What does the HEALTHCHECK instruction in a Dockerfile do, and how does Docker use the result?

Answer

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.

Why "running" is not enough

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.

Syntax and the options that matter

HEALTHCHECK [OPTIONS] CMD <command>

OptionDefaultMeaning
--interval30sTime between checks
--timeout30sA check taking longer counts as failed
--start-period0sGrace period for slow boots; failures here do not count
--start-interval5sCheck frequency during the start period (Docker Engine 25+)
--retries3Consecutive 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.

Who acts on the health status

  • Plain docker run: only reports it. An unhealthy container is not restarted automatically; --restart policies react to exits, not health.
  • Docker Compose: depends_on with condition: service_healthy makes, say, the API wait until Postgres is genuinely accepting connections.
  • Docker Swarm: replaces tasks that become unhealthy and waits for health during rolling updates.
  • Kubernetes: ignores HEALTHCHECK entirely. You define livenessProbe, readinessProbe and startupProbe in the pod spec instead.

Common mistakes

  • Using 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.
  • A health endpoint that checks every downstream dependency. If Redis blips, every API container goes unhealthy at once. Keep liveness shallow; check dependencies in readiness logic.
  • Forgetting --start-period for apps like JVM services that take 40 seconds to boot, so they are marked unhealthy before they ever start.
  • Expensive checks every few seconds that add real load.

Inspect results with docker inspect --format '{{json .State.Health}}' <container>, which shows the last few check outputs and the failing streak.

Health status lifecycle. Docker moves a container between health states based on consecutive check results.
  1. Container starts: starting: Checks run every start-interval during the start period.
  2. First passing check: healthy: Exit code 0 flips the status to healthy.
  3. Failures accumulate: Each failed or timed-out check increments the failing streak.
  4. Retries reached: unhealthy: Three consecutive failures by default.
  5. Orchestrator reacts: Swarm replaces the task; Compose blocks dependents; plain Docker only reports.

Code examples

Health check for a Node.js API without curl

# 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.

Waiting for a healthy database in Compose

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.

Key points

  • Exit 0 means healthy, exit 1 means unhealthy; states are starting, healthy and unhealthy.
  • Defaults: interval 30s, timeout 30s, start-period 0s, retries 3.
  • Plain Docker does not restart unhealthy containers; Swarm and Compose dependency conditions act on health.
  • Kubernetes ignores HEALTHCHECK and uses liveness, readiness and startup probes.
  • Make sure the check tool (curl, wget) actually exists in the final image.

Common follow-up questions

  • Will Docker restart a container that becomes unhealthy?
  • How do Kubernetes liveness and readiness probes differ from HEALTHCHECK?
  • What should a /healthz endpoint check, and what should it avoid?
  • How would you write a health check for a distroless image?

Interview tips

  • Say clearly that standalone Docker only reports health; many candidates wrongly claim it auto-restarts.
  • Mention start-period with a concrete slow-boot example like a JVM service.
  • Connecting HEALTHCHECK to Compose depends_on and Kubernetes probes shows you understand the whole pipeline.

Concepts covered

HEALTHCHECK, Health Status, start-period, depends_on service_healthy, Liveness Probe

More Docker & Containers questions