What is a Health Check
A health check is a probe that verifies whether a container is running correctly. A container's process might be alive (PID exists) but broken (deadlocked, out of connections, serving errors). Health checks detect this and trigger recovery — restarting or replacing the container.
How it works
Docker HEALTHCHECK — a command defined in the Dockerfile that runs periodically inside the container. If it returns exit code 0, the container is healthy. Non-zero means unhealthy. After a configurable number of consecutive failures, the container's status changes to unhealthy.
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
Kubernetes probes provide three types:
- Liveness probe — checks if the container is alive. Failure triggers a restart.
- Readiness probe — checks if the container can accept traffic. Failure removes it from service endpoints (no traffic routed to it, but it is not restarted).
- Startup probe — checks if the container has finished starting. Disables liveness and readiness probes until it succeeds.
Probe mechanisms: HTTP GET (check a URL), TCP socket (check if a port is open), exec (run a command inside the container), and gRPC (call a gRPC health service).
Why it matters
Without health checks, a crashed or hung container looks "running" to the runtime. Traffic continues to be routed to it, causing errors for users. Health checks enable self-healing — the orchestrator detects the problem and recovers automatically, often before users notice.
See How Containers Work for the container lifecycle that health checks monitor.