Sub topic 12
Adding health checks
This chapter assumes you can build and run an image, publish ports, and read a Compose file. It builds on the Node.js app and the Compose file from earlier chapters.
Why a running container is not always a working one
Docker marks a container as running as long as its main process is alive. A web server can be alive and still be stuck, out of database connections, or not yet listening. A health check is a command that Docker runs inside the container at intervals. Its exit code tells Docker whether the app is actually working.
- Exit code 0 means healthy.
- Exit code 1 means unhealthy.
Health check states
A container with a health check has a status in addition to its running state.
During the start period, failures do not count toward the retry limit. A single success during that period moves the container to healthy.
Adding a health endpoint
The check needs something to call. Add a cheap route to the app that returns 200 when the server can handle requests.
const express = require("express");
const app = express();
app.get("/health", (req, res) => {
res.status(200).json({ status: "ok" });
});
app.listen(3000, "0.0.0.0");Keep the route fast and free of side effects. Docker calls it every few seconds for the life of the container.
Declaring the check in the Dockerfile
Add a HEALTHCHECK instruction. The Alpine base image includes BusyBox wget, so you do not need to install anything.
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
CMD wget -q --spider http://127.0.0.1:3000/health || exit 1| Option | Meaning |
|---|---|
| `--interval` | Time between checks |
| `--timeout` | A check that runs longer than this counts as a failure |
| `--start-period` | Grace period for slow startup, where failures are not counted |
| `--retries` | Consecutive failures needed to mark the container unhealthy |
The command runs inside the container, so it uses 127.0.0.1 and the container port. The host port mapping does not matter.
Reading the status
Rebuild, run the container, and look at the status column.
docker build -t myapp:1.0 .
docker run --rm -d -p 3000:3000 --name myapp myapp:1.0
docker ps --format "table {{.Names}}\t{{.Status}}"
docker inspect --format "{{json .State.Health}}" myappdocker ps shows (health: starting), then (healthy). docker inspect shows the status and the output of the last few checks, which helps when a check fails.
Using health checks in Compose
Compose can define a check without changing the image. This matters for images you do not build, such as Postgres. The Compose file from the previous chapter already does this.
services:
web:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:secret@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
POSTGRES_DB: app
volumes:
- dbdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 5
volumes:
dbdata:Without condition: service_healthy, depends_on only waits until the database container has started. Postgres needs several more seconds before it accepts connections, so the app could fail on its first query. With the condition, Compose starts web only after pg_isready succeeds.
Common mistakes
- The check command is not installed in the image. A minimal image without
wgetorcurlfails every check. Use a tool the image has, or run a small Node script. - The check tests the wrong address. Use the container port, not the published host port.
- The check does too much. A route that runs heavy queries adds load and can fail for reasons unrelated to the app.
- No start period. A slow app is marked unhealthy before it finishes starting.
Exercise
In Compose, the web service has depends_on: db with no condition, and db has a working health check. The app still crashes on startup with a connection error. Why, and what is the fix?
Show answer
Without a condition, depends_on only waits for the db container to start, not for Postgres to accept connections. The fix is to use the long form with condition: service_healthy, so Compose starts web only after the pg_isready check passes.