Signal Handling in Docker and Kubernetes: PID 1 and Graceful Shutdown
Why containers ignore SIGTERM, how PID 1 changes signal behaviour, exec vs shell form, tini and dumb-init, and Kubernetes grace periods and preStop hooks.
Published November 27, 2026
You run docker stop, and ten seconds later the container is killed forcefully. Your application never saw the shutdown signal, connections were cut, and jobs were lost. The cause is almost always the same: the process running as PID 1 inside the container is not handling signals the way you think. This guide explains why and how to fix it in Docker and Kubernetes.
The concept
Inside a container, your first process becomes PID 1 in its PID namespace. Linux treats PID 1 specially: the kernel does not apply default signal actions to it. A normal process with no SIGTERM handler dies on SIGTERM; PID 1 with no handler simply ignores it. So an application that relies on default behaviour will not stop until SIGKILL arrives after the timeout.
The second trap is the shell form of CMD and ENTRYPOINT. CMD npm start in a Dockerfile actually runs /bin/sh -c "npm start". Now sh is PID 1, and sh does not forward SIGTERM to its children. npm itself can also fail to forward signals to node. The fix is the exec form (CMD ["node", "server.js"]) so your app is PID 1, or a tiny init such as tini that forwards signals and reaps zombies.
Kubernetes adds orchestration timing. When a pod is deleted, the kubelet runs any preStop hook, then sends SIGTERM to PID 1 in each container, waits terminationGracePeriodSeconds (30 seconds by default), then sends SIGKILL. Meanwhile the pod is removed from Service endpoints, which happens in parallel, so a short delay before shutting down avoids dropping requests that are still being routed to the pod.
The broken case
The fixed case
Dockerfile forms compared
| Dockerfile | PID 1 | Gets SIGTERM? |
|---|---|---|
| CMD npm start | /bin/sh | No: sh does not forward |
| CMD ["npm", "start"] | npm | Unreliable forwarding to node |
| CMD ["node", "server.js"] | node | Yes, if the app installs a handler |
| ENTRYPOINT ["tini", "--"] + CMD ["node", "server.js"] | tini | Yes, forwarded; zombies reaped |
| docker run --init ... | docker-init (tini) | Yes, without changing the image |
Kubernetes shutdown timeline
- Pod marked Terminating; removal from endpoints starts in parallel
- preStop hook runs (for example sleep 5 so load balancers catch up)
- SIGTERM sent to PID 1 of every container
- App stops accepting work, finishes in-flight requests, exits
- After terminationGracePeriodSeconds, anything left gets SIGKILL
Practical examples
Dockerfile with tini and exec form
tini is tiny, and Alpine/Debian ship it as a package.
FROM node:22-slim
RUN apt-get update && apt-get install -y --no-install-recommends tini && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY . .
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["node", "server.js"]
STOPSIGNAL SIGTERMEntrypoint script that keeps signals working
Do setup in shell, then exec so the app replaces the shell as PID 1.
#!/bin/sh
set -e
./migrate.sh
exec "$@" # app becomes PID 1 and receives signals directlyKubernetes grace period and preStop
Give the app time to drain and let endpoints update first.
spec:
terminationGracePeriodSeconds: 45
containers:
- name: api
image: example/api:1.4
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 5"]Common mistakes
- Using shell-form CMD and then debugging why shutdown always takes exactly 10 seconds.
- Writing an entrypoint script that runs the app without exec, leaving the shell as PID 1.
- Setting a grace period shorter than the longest request or job.
- Exiting instantly on SIGTERM in Kubernetes, while the load balancer still sends traffic to the pod.
- Forgetting that PID 1 must also reap zombie children if it spawns subprocesses.
Key takeaways
- PID 1 ignores signals that have no handler, so apps must handle SIGTERM explicitly.
- Use exec-form CMD, exec in entrypoint scripts, or tini/--init.
- Kubernetes sends SIGTERM, waits terminationGracePeriodSeconds, then SIGKILL.
- A short preStop sleep prevents dropped requests during endpoint updates.
FAQ
How do I test signal handling locally?
Run docker stop and time it: docker stop -t 30 name. If it always takes the full timeout, signals are not reaching your app. docker kill -s TERM name sends a signal without the timeout.
What does STOPSIGNAL do?
It changes the signal docker stop sends. For example nginx images use SIGQUIT for a graceful shutdown.
Do I need tini if my app handles SIGTERM?
Not for signals if the app is PID 1 via exec form. tini is still useful if your app spawns child processes, because it reaps zombies.