Skip to content

15. Healthchecks & Probes

Just because a container’s main process (PID 1) is “Running” does not mean the application is actually working. The web server might be deadlocked, the database connection might have dropped, or the application might still be taking 60 seconds to boot up.

Docker provides mechanisms to actively interrogate containers to determine their true health, and integrates with enterprise monitoring stacks to provide deep visibility.

A Healthcheck is a command that Docker periodically runs inside the container to test if the application is functioning correctly.

You can bake the healthcheck directly into the image itself.

# Curl the web server every 30 seconds. If it fails, exit with code 1.
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/ || exit 1

Alternatively, you can define or override healthchecks inside a compose.yaml file, which is often preferred because it allows you to configure timeouts differently for dev vs. production.

services:
api:
image: my-api
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost/healthz"]
interval: 10s
timeout: 5s
retries: 3

When a healthcheck is configured, the container cycles through specific states visible in docker ps:

  • starting: The container is booting up. Healthchecks are running but have not passed yet.
  • healthy: The healthcheck command successfully executed and returned a 0 exit code.
  • unhealthy: The healthcheck command failed (non-zero exit code) consecutively across the defined number of retries.

To write effective healthchecks, your application code should expose specific endpoints (like /healthz or /ready) tailored for different types of checks.

Answers the question: “Is the application completely frozen or deadlocked?”

  • If a liveness check fails, the orchestrator (like Docker Swarm or Kubernetes) will forcefully kill the container and restart it.
  • Example: A simple ping to ensure the HTTP server responds.

Answers the question: “Is the application ready to receive user traffic?”

  • An application might be alive, but it might have lost its connection to the database. It shouldn’t be killed, but it shouldn’t receive user traffic either.
  • If a readiness check fails, the orchestrator temporarily removes the container from the load balancer until it recovers.
  • Example: An endpoint that actively pings the backend database.

Answers the question: “Has the application finished its heavy initial boot sequence?”

  • Massive Java applications or databases might take 3 minutes to load caches into memory. Startup checks ensure Liveness probes don’t accidentally kill the container while it’s still booting.

While docker stats is great for local debugging, production environments require centralized monitoring. You don’t need to master these tools immediately, but you must understand how they piece together.

The industry standard for container monitoring is the Prometheus & Grafana stack.

An open-source daemon created by Google. You run cAdvisor as a background container on your Docker host. It reaches deep into the Linux kernel and extracts highly granular metrics (CPU, Memory, Network TX/RX) about every single running container on that machine, exposing them on an HTTP endpoint.

Another background daemon that exposes metrics about the physical host machine itself (e.g., how full the host’s physical hard drive is, or host CPU temperature).

The time-series database. Prometheus is configured to reach out over the network and “scrape” the HTTP endpoints exposed by cAdvisor and Node Exporter every 15 seconds. It stores all this historical metric data efficiently.

The visualization layer. Grafana connects to Prometheus and translates the raw metrics into beautiful, real-time dashboards and charts. You can configure Grafana to send you a Slack or PagerDuty alert if a specific container’s CPU usage spikes above 90% for more than 5 minutes.