Skip to content

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 node

Shell 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

  • STOPSIGNAL changes which signal stop sends — SIGQUIT, for instance, is nginx's graceful-shutdown signal.
  • Kubernetes does the same dance on pod deletion; terminationGracePeriodSeconds is the identical knob.
  • tini -g forwards the signal to the whole process group, not just the direct child — the flag for wrapper scripts that spawn workers.
  • docker stop -t 0 is SIGKILL in a costume; if every stop ends in kill, the handler or the entrypoint form is broken — not the timeout.
  • docker run --rm deletes the container on exit — zombies and all — the correct end state for batch jobs.
  • Healthchecks cover the other gap docker stop cannot: a container can run while the app inside is dead; docker ps shows (healthy) or nothing.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.