What does the HEALTHCHECK instruction do in a Dockerfile?
HEALTHCHECK tells Docker how to test that the app inside is actually working. Docker runs the command at intervals and marks the container healthy or unhealthy, visible in docker ps. It does not restart it by itself.
Explain it simply
A running process is not the same as a working app. A shop with open shutters may have nobody at the counter. A health check is a peon who periodically knocks and asks 'anyone there?'.
You give Docker a command; exit code 0 means healthy, 1 means unhealthy. Options: --interval (how often), --timeout, --start-period (grace time for slow boots) and --retries (failures before unhealthy). docker ps then shows (healthy) or (unhealthy). Plain Docker only reports the status. Docker Swarm and Compose depends_on: condition: service_healthy can act on it. Kubernetes ignores this instruction and uses its own liveness and readiness probes.
The command must exist inside the image, so minimal images may need wget or curl added.
Example
FROM nginx:alpine
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD wget -qO- http://localhost/ || exit 1What to say in the interview
"HEALTHCHECK runs a command at intervals to see whether the app really works, and marks the container healthy or unhealthy. Docker itself only reports it; orchestrators or Compose conditions can act on it. Kubernetes uses probes instead."
Common mistakes
- Expecting plain docker to restart an unhealthy container automatically.