Kill vs. Stop

Difficulty: Beginner

Question

What is the difference between docker stop and docker kill, and why does a container sometimes take 10 seconds to stop?

Answer

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.

Side by side

docker stopdocker kill
Signal sentSTOPSIGNAL, default SIGTERMSIGKILL by default, any signal with -s
Waits for exitYes, --timeout / -t, default 10sNo
FallbackSIGKILL after the timeoutNone needed
Typical exit code0 or 143 if handled, 137 if force-killed137 (128 + 9)
Use whenNormal shutdowns and deploysHung 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.

Why graceful shutdown matters

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.

The 10-second hang explained

If docker stop always takes exactly 10 seconds, your app is ignoring SIGTERM. Common causes:

  1. Shell form CMD. 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"].
  2. PID 1 signal semantics. The kernel does not apply default signal actions to PID 1, so a process with no SIGTERM handler simply ignores it. Add a handler, or run with --init so tini becomes PID 1 and forwards signals.
  3. Wrapper scripts. An entrypoint.sh must end with exec "$@" so the real app replaces the shell.
  4. Wrong signal. Some apps expect a different signal (Nginx graceful quit is SIGQUIT). Set STOPSIGNAL in the Dockerfile.

Tuning the timeout

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.

What docker stop does. SIGKILL is only the fallback when the app ignores SIGTERM for the whole grace period.
  1. Docker CLI → Docker daemon: docker stop api
  2. Docker daemon → PID 1 in container: SIGTERM (or STOPSIGNAL)
  3. PID 1 in container → PID 1 in container: Drain requests, close DB pool
  4. PID 1 in container → Docker daemon: Exits within 10s
  5. Docker daemon → PID 1 in container: Else SIGKILL after timeout

Code examples

Handling SIGTERM in Node.js

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.

Comparing the commands

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.

Key points

  • stop sends SIGTERM (or STOPSIGNAL), waits 10s by default, then SIGKILL.
  • kill sends SIGKILL immediately, or any signal via -s.
  • Exit code 137 means SIGKILL; 143 means terminated by SIGTERM without a clean exit.
  • Shell-form CMD and missing signal handlers on PID 1 cause the 10-second hang.
  • Fix with exec-form CMD, exec in entrypoint scripts, --init, or STOPSIGNAL.

Common follow-up questions

  • Why does PID 1 ignore SIGTERM when it has no handler?
  • What does tini or docker run --init do?
  • How does Kubernetes terminate a pod, and where do preStop hooks fit?
  • What does exit code 137 tell you, and how is it different from an OOM kill?

Interview tips

  • State the full stop sequence including the 10-second fallback; that is the core of the answer.
  • Volunteer the shell-form CMD and PID 1 explanation; it is the follow-up interviewers almost always ask.
  • Connect graceful shutdown to a business consequence such as half-processed payments.

Concepts covered

SIGTERM, SIGKILL, Graceful Shutdown, STOPSIGNAL, PID 1

More Docker & Containers questions