Docker interview question · Most asked
Why does PID 1 matter in containers, and what problems can it cause?
Summary
The container's main process runs as PID 1, which Linux treats specially: it does not get default signal handlers (signals it does not handle are ignored, so SIGTERM may do nothing), and it is responsible for reaping orphaned child processes, which otherwise become zombies.
Longer Answer
Symptoms: containers take the full stop timeout (10 s in Docker, 30 s grace period in Kubernetes) to stop and are then killed, losing in-flight work; zombie processes accumulate when the app spawns children. Fixes: handle SIGTERM in the application, use the exec form, and run a tiny init when needed (docker run --init, which uses tini, or tini/dumb-init as the entrypoint).
Key Points
- PID 1 lacks default signal handlers.
- Must reap zombie processes.
- Handle SIGTERM; exec form.
- --init / tini for reaping and forwarding.
More most-asked Docker questions
- What is a container, and how does it differ from a virtual machine?
- How does Docker build caching work, and how do you order a Dockerfile to use it well?
- What is the difference between an image and a container, and how do image layers work?
- CMD or ENTRYPOINT: what is the difference, and what are exec and shell forms?
- What are Dockerfile best practices for production images?
- ARG or ENV: what is the difference, and how do you pass secrets into a build safely?
- What are multi-stage builds, and why use them?
- How do you secure Docker containers and images?