Lesson 9 of 19 · 20m
PID 1, Signals, and the Ten-Second Stop
PID 1 ignores unhandled signals and inherits every orphan — shell form hides SIGTERM from your app, exec form plus a handler is the fix.
Every deploy takes ten extra seconds and nobody knows why. docker stop is called, nothing happens, then the container dies all at once — mid-request, connections dropped. The mechanism: docker stop sends SIGTERM to the container's PID 1, waits the stop timeout (default ten seconds — --stop-timeout on run, stop_grace_period in Compose), then sends SIGKILL. Your app never saw the SIGTERM, and the reason is that PID 1 is not a normal process — and odds are your app is not running as it anyway.
PID 1 plays by different rules
Inside a container's PID namespace, PID 1 has two unusual properties. First, the kernel delivers a signal to it only if the process installed a handler for that signal — the default-kill disposition does not apply to the namespace init. SIGTERM sent to a PID-1 process with no handler is silently ignored. Second, orphaned grandchildren reparent to PID 1, and reaping them — wait()ing on dead children — is PID 1's job. A PID 1 that never reaps accumulates zombies until the pids cgroup fills. This is why containers need init semantics: somebody has to answer signals and collect corpses. It is also why docker stop and docker kill are different operations — stop negotiates, kill does not.
Shell form versus exec form
The Dockerfile instruction decides who becomes PID 1:
ENTRYPOINT node server.js # shell form: PID 1 is /bin/sh -c 'node server.js'
ENTRYPOINT ["node", "server.js"] # exec form: PID 1 is nodeShell form wraps your command in sh -c. The shell becomes PID 1 and your app is its child — signals land on the shell, and sh does not forward them to children. Every docker stop burns the full ten seconds and SIGKILLs the app. Exec form runs your binary as PID 1 directly. This is why official images end their entrypoint scripts with exec "$@": exec replaces the shell with your process, so the app becomes PID 1 rather than staying its child.
The instruction pair also decides what docker run img --debug means: run arguments replace CMD entirely but append to ENTRYPOINT. ENTRYPOINT ["python","app.py"] plus CMD ["--serve"], then docker run img --debug, executes python app.py --debug.
Handling the signal is still on you
Exec form fixes delivery, not handling. Node, Python, and most frameworks exit on SIGTERM only if something registered a handler — as PID 1, no handler means the signal evaporates:
process.on("SIGTERM", () => server.close(() => process.exit(0)));server.close() stops accepting connections, drains in-flight requests, then exits — that is a graceful shutdown. Without it, an exec-form Node app still eats the SIGKILL ten seconds later.
--init for apps that will not cooperate
Fork-happy or legacy apps need a real init. docker run --init inserts tini — a ~20 KB init embedded in dockerd — as PID 1: it forwards signals to your app and reaps orphans. Compose exposes it as init: true; in an image, ENTRYPOINT ["tini", "--", "node", "server.js"]. It is also the cure for the zombie-accumulation failure mode on anything that spawns subprocesses.
The mirror-image failure is the app that daemonizes — double-forks, detaches, lets the launcher exit. PID 1 gone means the container reads as stopped while the real process orphans itself and keeps running inside the namespace; a later docker stop signals a launcher that already left. Containers want foreground processes. Whatever habit or init script says otherwise is the thing to delete.
Try this
Build the same trivial HTTP server twice — once with shell-form ENTRYPOINT, once exec form — and time docker stop each. Shell form takes ten seconds every time. Then delete the SIGTERM handler from the exec-form version and watch the ten seconds return.
Field notes
STOPSIGNALchanges which signal stop sends —SIGQUIT, for instance, is nginx's graceful-shutdown signal.- Kubernetes does the same dance on pod deletion;
terminationGracePeriodSecondsis the identical knob. tini -gforwards the signal to the whole process group, not just the direct child — the flag for wrapper scripts that spawn workers.docker stop -t 0is SIGKILL in a costume; if every stop ends in kill, the handler or the entrypoint form is broken — not the timeout.docker run --rmdeletes the container on exit — zombies and all — the correct end state for batch jobs.- Healthchecks cover the other gap
docker stopcannot: a container can run while the app inside is dead;docker psshows(healthy)or nothing.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
What is Kubernetes? Container Orchestration at Scale
Kubernetes runs your Docker containers across a cluster of machines, handles scheduling, health checking, rolling deployments, auto-scaling, and self-healing. When your application outgrows a single server, Kubernetes is how it runs on ten.
What is Docker? Containers, Images, and Why Every Developer Needs to Know This
Docker packages your application and everything it needs to run into a container that behaves identically on your laptop, your teammate's machine, and the production server. "It works on my machine" stops being a problem. Here is how it actually works and what production usage looks like.