🐳 Docker Intermediate

What is a Docker healthcheck and how does it differ from a liveness probe?

Why Interviewers Ask This

This tests whether you can apply Docker knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Both Docker healthchecks and Kubernetes liveness/readiness probes check container health, but they operate in different contexts with different consequences. Docker HEALTHCHECK (Dockerfile/Compose): runs a command inside the container periodically; updates the container's health status (healthy/unhealthy/starting); in Docker Swarm, unhealthy tasks are replaced; Docker Compose respects health status for depends_on with condition: service_healthy. Example: HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost:3000/health || exit 1. Kubernetes Probes: (1) Liveness probe: determines if the container is running; if it fails, Kubernetes kills and restarts the container — for detecting deadlocks; (2) Readiness probe: determines if the container is ready to serve traffic; if it fails, Kubernetes removes the Pod from Service endpoints (stops sending traffic) but doesn't restart it — for slow startup or temporary unavailability; (3) Startup probe: protects slow-starting containers from liveness probe failures during initialization. Docker healthchecks are simpler; Kubernetes probes are more powerful and fine-grained. In Kubernetes deployments, define both liveness and readiness probes at the Pod spec level, not in the Dockerfile (though Docker healthcheck metadata can inform K8s configuration).

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Docker answers easy to follow.