What is init in Docker containers?
Answer
Docker containers need a proper init process (PID 1) to handle zombie processes and correctly forward OS signals. Problem: when your application forks child processes (common in Node.js with worker processes, or shell scripts that spawn children), and those children exit, they become "zombie" processes if the parent doesn't collect their exit status. Normally, the Linux init process (PID 1) reaps orphaned processes. But in a container, PID 1 is your application process, which typically doesn't implement signal handling or zombie reaping. Additionally, signals (SIGTERM for graceful shutdown) sent to PID 1 may be ignored if PID 1 doesn't explicitly handle them. Solutions: (1) --init flag: docker run --init myapp — Docker injects tini (a tiny init system) as PID 1; your app becomes PID 2. Tini forwards signals and reaps zombies; (2) Include tini in Dockerfile: RUN apk add --no-cache tini\nENTRYPOINT ["/sbin/tini", "--"]\nCMD ["node", "app.js"]; (3) dumb-init: similar to tini, by Yelp; (4) Use exec form for CMD/ENTRYPOINT (JSON array) — ensures your process IS PID 1 and receives signals directly, but still doesn't handle zombie reaping; (5) For simple single-process apps without forking, exec form is sufficient.