Skip to main content
Docker

Container graceful shutdown: SIGTERM is the beginning, not the shutdown

Handle SIGTERM, stop accepting new work, drain in-flight requests, and exit before the orchestrator sends SIGKILL. The signal handler alone is not a graceful shutdown plan.

Thien Nguyen
By Thien Nguyen
Updated July 16, 2026 · 1 min read

Deploys should not create a small, random packet-loss window. When a container receives SIGTERM, it needs to stop becoming eligible for new work, finish what it already owns, and exit before the platform's grace period expires.

Short answer: make your actual application process receive SIGTERM, flip readiness to false, close listeners, drain work with a deadline, then close database and queue connections. SIGKILL cannot be handled, so anything left after the deadline is simply abandoned.

A Node HTTP sequence

let draining = false;
process.on("SIGTERM", async () => {
  draining = true;                 // readiness endpoint now returns 503
  server.close();                  // no new HTTP connections
  await Promise.race([drainJobs(), timeout(25_000)]);
  await pool.end();
  process.exit(0);
});
OrderWhy it matters
Fail readinessLoad balancers stop selecting this instance
Stop accepting connectionsNew requests do not extend the drain forever
Finish or hand off jobsAvoid duplicate or half-complete work
Close dependenciesReleases database and broker resources
Exit before grace periodAvoids an uncatchable kill

Do not put your app behind sh -c as PID 1 and assume signals arrive where you expect. Use an exec-form Docker CMD, or an init process that forwards signals correctly.

Test the unhappy path

Run a request that takes ten seconds, send SIGTERM halfway through, and inspect the client result. Then repeat with a job consumer and a database transaction. A shutdown hook that only logs “received SIGTERM” has tested nothing.

Graceful shutdown is backpressure at deployment time: communicate that you are leaving, stop accepting responsibility, then leave cleanly.

Cover photo by Wolfgang Weiser on Pexels.

References

Primary documentation and specifications checked when this article was last updated.

DockerKubernetesReliability

Related articles

All articles