Difficulty: Beginner
What is the difference between docker stop and docker kill, and why does a container sometimes take 10 seconds to stop?
docker stop asks the container to shut down gracefully: it sends SIGTERM (or the image's STOPSIGNAL) to PID 1, waits up to 10 seconds by default, and only then sends SIGKILL. docker kill sends SIGKILL immediately, so the process dies without any chance to finish requests, flush buffers or close connections.
docker stop | docker kill | |
|---|---|---|
| Signal sent | STOPSIGNAL, default SIGTERM | SIGKILL by default, any signal with -s |
| Waits for exit | Yes, --timeout / -t, default 10s | No |
| Fallback | SIGKILL after the timeout | None needed |
| Typical exit code | 0 or 143 if handled, 137 if force-killed | 137 (128 + 9) |
| Use when | Normal shutdowns and deploys | Hung containers, or sending a custom signal |
docker kill is not only for killing: docker kill -s SIGHUP nginx tells Nginx to reload its config without stopping.
When a payment API gets SIGTERM it should stop accepting new requests, let in-flight ones finish, commit or roll back database transactions, ack or requeue messages, and then exit. A SIGKILL mid-request can leave an order marked "paid" in one system and "pending" in another. Orchestrators rely on this too: Kubernetes sends SIGTERM and waits terminationGracePeriodSeconds (30s by default) before killing.
If docker stop always takes exactly 10 seconds, your app is ignoring SIGTERM. Common causes:
CMD node server.js runs as /bin/sh -c "node server.js". The shell is PID 1 and does not forward signals to Node. Use exec form: CMD ["node", "server.js"].SIGTERM handler simply ignores it. Add a handler, or run with --init so tini becomes PID 1 and forwards signals.entrypoint.sh must end with exec "$@" so the real app replaces the shell.SIGQUIT). Set STOPSIGNAL in the Dockerfile.Raise the grace period for apps that drain slowly: docker stop -t 30 api, stop_grace_period: 30s in Compose, or --stop-timeout at docker run. Lowering it to 0 makes stop behave like kill.
const express = require('express');
const app = express();
app.get('/healthz', (req, res) => res.send('ok'));
const server = app.listen(3000);
process.on('SIGTERM', () => {
console.log('SIGTERM received, draining connections');
// Stop accepting new connections; finish in-flight requests
server.close(async () => {
await db.end(); // close the Postgres pool
process.exit(0);
});
// Safety net in case something hangs
setTimeout(() => process.exit(1), 8000).unref();
});
With exec-form CMD this handler receives SIGTERM directly, so the container exits cleanly in well under the 10-second window.
docker stop -t 30 api # graceful, up to 30s
docker kill api # immediate SIGKILL
docker kill -s SIGHUP proxy # send a reload signal instead
# See how each container ended
docker inspect api --format '{{.State.ExitCode}}'
Exit code 137 means the process was killed by signal 9; a clean graceful shutdown usually exits 0.
SIGTERM, SIGKILL, Graceful Shutdown, STOPSIGNAL, PID 1