Healthchecks

A healthcheck is a command Docker runs regularly inside the container to know whether the app really works. The container's status becomes starting, healthy or unhealthy.

How it works

HEALTHCHECK in a Dockerfile or healthcheck: in Compose sets the test, its interval, timeout, retries and start period. The test runs inside the container, so the tool it calls (curl, wget, pg_isready) must exist in the image. In Compose, depends_on with condition: service_healthy waits for it.

What breaks

Docker alone does not restart an unhealthy container: the status informs, and orchestrators like Swarm act on it. A test that calls a missing tool marks a working app as unhealthy, and a test slower than its timeout fails every time.

In compose.yaml

services:
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 3s
      retries: 5
      start_period: 10s

See it break, then fix it

Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.