BlogCI/CD

HEALTHCHECK and graceful shutdown in containers

Tell the orchestrator when your container is actually healthy, and handle SIGTERM so deploys don't drop requests.

Aug 27, 2024·8 min readIntermediate

An orchestrator only knows your container is 'running' — not whether it can actually serve. A HEALTHCHECK closes that gap, and handling SIGTERM closes the other one: a deploy that does not drain in-flight requests drops user traffic on every rollout.

Tell the platform you are healthy

Dockerfile
HEALTHCHECK --interval=15s --timeout=3s --retries=3 \
CMD wget -qO- http://localhost:8080/healthz || exit 1
bash — health surfaces in statuslive
docker ps
STATUS
Up 2 minutes (healthy)

Shut down gracefully

On a rollout the runtime sends SIGTERM, waits a grace period, then SIGKILL. Catch SIGTERM, stop accepting new work, finish in-flight requests, then exit.

entrypoint
# exec form so your process is PID 1 and receives signals
ENTRYPOINT ["node", "server.js"]
# in the app: server.close() on SIGTERM, then process.exit(0)
Shell-form CMD swallows signals
CMD node server.js (shell form) runs under /bin/sh as PID 1, which does not forward SIGTERM. Use exec form, or SIGTERM never reaches your app and every deploy hard-kills it.
Go deeper in a courseDocker in depthHealthchecks, signals, and running containers reliably.View course

Related posts