Containers and Signal Handling: Why You Need to Care About PID 1
When running applications in Docker containers, many developers overlook a critical detail: what process runs as PID 1. This seemingly minor choice can lead to unresponsive containers, resource leaks, and unexpected behavior during shutdown.
Why PID 1 is Special
In Linux, the kernel treats PID 1 differently from all other processes. It's the "init" process that bootstraps the system and has two critical responsibilities:
Signal handling: The kernel doesn't deliver certain signals (like SIGTERM) to PID 1 unless it explicitly registers handlers for them.
Process reaping: PID 1 must clean up zombie processes by calling waitpid()
on dead children.
When you run a container with:
CMD ["./my-app"]
Your application becomes PID 1, inheriting these kernel expectations whether it's designed for them or not.