Node.js › Docker & Deployment

What is the purpose of a HEALTHCHECK instruction in a Dockerfile for a Node.js application?

Intermediate

Answer

HEALTHCHECK defines a command Docker periodically runs inside the container to determine if the application is actually functioning correctly, not just whether the process is technically still running -- Docker (and orchestrators reading this status) can use a failing health check to automatically restart an unhealthy container or avoid routing traffic to it, providing an additional layer of self-healing beyond simply checking if the process exists.
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
  CMD node healthcheck.js || exit 1

// healthcheck.js: makes a request to the app's own /health endpoint
// and exits with a non-zero code if it doesn't respond successfully
Real-world example A container that had technically stayed 'running' while its underlying HTTP server had silently crashed internally (leaving only a zombie process) is caught by a HEALTHCHECK that actively probes the /health endpoint, letting Docker detect and restart the actually-unhealthy container.

Common follow-ups: How does a Dockerfile-level HEALTHCHECK relate to and differ from Kubernetes's own separate liveness/readiness probes?;What happens to a container marked 'unhealthy' by Docker if nothing is configured to act on that status?