Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 133/ 160
07. CI/CD, Registries & Production Operations

Graceful Shutdowns, SIGTERM vs SIGKILL & Signal Handling

Handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL.

IntermediateSIGTERM · SIGKILL3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Design shutdown latency around the slowest legitimate operation and verify the signal reaches the actual service process.

Think of it like…

A restaurant stops seating customers, serves the existing tables, closes the kitchen and only then turns off the lights.

Where you’ve seen it

In practice, STOPSIGNAL SIGTERM; docker stop --time 45 api is the concrete move for this job: handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL. The release is not accepted until open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds confirms the result.

Words you’ll hear

SIGTERM
The implementation control in STOPSIGNAL SIGTERM; docker stop --time 45 api for handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL.
SIGKILL
The result to observe with open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds, rather than infer from a successful command alone.
Grace Period
The production boundary exposed when setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.

Visual diagram

Loading interactive diagram

Theory & deep dive

SIGTERM control path

The concrete mechanism is STOPSIGNAL SIGTERM; docker stop --time 45 api. The observable result is open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds; if those disagree, inspect the runtime evidence before changing unrelated settings.

Handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL. Keep SIGTERM, SIGKILL and Grace Period in the same deployment record so the intent and the observed state cannot drift apart.

Concrete SIGTERM setup

STOPSIGNAL SIGTERM; docker stop --time 45 api

Key takeaway: Prove SIGKILL with open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds, not with command success alone.

Grace Period failure boundary

A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence. The narrow failure to design for is setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.

Accept this compromise explicitly: Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes. The controlled check-open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.

Key takeaway: Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence.

Design shutdown latency around the slowest legitimate operation and verify the signal reaches the actual service process.
Key numbers at this step
Intent
SIGTERM

When to use it & trade-offs

What it is

docker stop sends SIGTERM to PID 1 and waits the stop grace period before SIGKILL. The app should stop accepting work, drain or finish in-flight work, flush state and exit; orchestration readiness and connection timeouts must line up with the grace window.

Why it matters: A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence.

When to reach for it

  • The fit is strongest for this job: handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL. Pair it with open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds in the runbook.
  • Production SIGTERM changes should make SIGKILL observable through STOPSIGNAL SIGTERM; docker stop --time 45 api, rather than trusting the runtime default.
  • Keep a rollback example for the case where setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.

When to avoid or be careful

  • Do not accept command success while the system can still fail by setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.
  • Avoid erasing Grace Period protection merely to dodge this cost: “Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes”.
  • Do not replace the evidence path-open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds-with a screenshot or a mutable tag.

STOPSIGNAL SIGTERM; docker stop --time 45 api: practical choices

Explicit SIGTERM
Best for: Handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL, with a reviewed runbook.

Strengths: docker stop sends SIGTERM to PID 1 and waits the stop grace period before SIGKILL. The app should stop accepting work, drain or finish in-flight work, flush state and exit; orchestration readiness and connection timeouts must line up with the grace window.

Watch out: Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes.

Ambient default
Best for: Short-lived SIGKILL experiments with low blast radius.

Strengths: It can begin with STOPSIGNAL SIGTERM; docker stop --time 45 api and fewer surrounding controls.

Watch out: It silently accepts setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically, which makes the result hard to reproduce after promotion.

Managed Grace Period
Best for: Teams that centralize Grace Period policy across many workloads.

Strengths: A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence.

Watch out: The platform adds an ownership and debugging layer; retain open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds as the direct check.

Interview signals & numbers to know

When this topic comes up

  • STOPSIGNAL SIGTERM; docker stop --time 45 api
  • open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds
  • Failure signature: setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically
  • Trade-off: Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes.

Benchmarks & rules of thumb

45 seconds -
example stop grace
SIGTERM 15 -
signal number 15 on typical Linux
SIGKILL 9 -
uncatchable escalation

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful STOPSIGNAL SIGTERM; docker stop --time 45 api as proof and skipping open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds.
  • Setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically
  • Optimizing SIGTERM without accepting that graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes.

Likely interviewer follow-ups

  • Why does STOPSIGNAL SIGTERM; docker stop --time 45 api address SIGTERM here rather than through application code?
  • How would you distinguish healthy SIGKILL from the failure “setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically” using open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds?
  • When would this trade-off-“Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes”-justify a different Grace Period design?
Interview tip: Give one concrete operation-STOPSIGNAL SIGTERM; docker stop --time 45 api-and one proof-open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds. Then name the boundary: setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Graceful Shutdowns, SIGTERM vs SIGKILL & Signal Handling with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Container Metrics & Monitoring (cAdvisor, Prometheus, Grafana)
Next topic
The PID 1 Problem & Using Init Systems (tini, --init)