Difficulty: Intermediate
What is the difference between docker stop and docker kill, and how do you make a containerized application shut down gracefully?
docker stop sends the container's stop signal (SIGTERM by default) to the main process, waits for a grace period (10 seconds by default), and only then sends SIGKILL. docker kill sends SIGKILL immediately by default, so the process has no chance to finish requests, flush buffers or close database connections. Graceful shutdown depends on your application actually receiving and handling SIGTERM as PID 1.
docker stop api sends SIGTERM (or whatever STOPSIGNAL the image or --stop-signal specifies) to PID 1 in the container.docker stop -t 30, --stop-timeout on docker run, or stop_grace_period in Compose.SIGKILL, which cannot be caught.docker kill api skips straight to SIGKILL. You can send other signals with docker kill -s SIGHUP api, for example to make nginx reload its config.
| docker stop | docker kill | |
|---|---|---|
| Default signal | SIGTERM (STOPSIGNAL) | SIGKILL |
| Grace period | 10s, then SIGKILL | None |
| Typical exit code | 0 or 143 if handled | 137 (128 + 9) |
| Use for | Deploys, normal stops | Hung containers, sending custom signals |
CMD node server.js runs /bin/sh -c ..., so the shell is PID 1 and may not forward the signal. Use exec form CMD ["node", "server.js"].SIGTERM handler, the signal is effectively ignored and Docker waits the full timeout before killing it. That is the classic "every deploy takes exactly 10 seconds per container" symptom.npm start adds an npm process in front of node. Run node directly.--init (or init: true in Compose), which uses a tiny init (tini) as PID 1 that forwards signals and reaps zombie processes.For an order service at a food delivery company, a clean shutdown is: stop accepting new connections, let in-flight HTTP requests finish, stop pulling from the queue and finish or requeue the current message, close DB pools, then exit with code 0. In Kubernetes the same thing happens on every rolling update: the kubelet sends SIGTERM, waits terminationGracePeriodSeconds (30s default), then SIGKILL. Getting this wrong shows up as dropped requests and half-processed payments during deploys.
Exit code 137 does not always mean someone ran docker kill; it also appears when the kernel OOM killer terminates the process. Check docker inspect --format '{{.State.OOMKilled}}' to tell them apart.
const http = require('node:http');
const server = http.createServer((req, res) => {
setTimeout(() => res.end('ok'), 2000); // simulate slow work
});
server.listen(3000);
function shutdown(signal) {
console.log(`${signal} received, draining connections`);
server.close(async () => {
// await db.end(); await queue.close();
console.log('clean exit');
process.exit(0);
});
// Safety net: exit before Docker's SIGKILL arrives
setTimeout(() => process.exit(1), 25_000).unref();
}
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
server.close stops new connections and waits for in-flight ones; the timer ensures the process exits before the grace period runs out.
docker run -d --name api --init --stop-timeout 30 my-api:1.0
docker stop api # SIGTERM, up to 30s, then SIGKILL
docker kill api # immediate SIGKILL
docker kill -s SIGHUP web # send a custom signal
docker inspect api --format \
'exit={{.State.ExitCode}} oom={{.State.OOMKilled}}'
--init adds a signal-forwarding init process, and the inspect output tells you whether the exit was clean, killed or OOM.
SIGTERM, SIGKILL, Stop Timeout, PID 1, Exit Code 137