Docker's --init Flag Fixes Zombie Processes
The Problem
My Node.js container wouldn’t stop gracefully. docker stop took 10 seconds (the default timeout) before force-killing it. Child processes weren’t being reaped.
The Solution
docker run --init my-image
Or in Docker Compose:
services:
app:
image: my-image
init: true
Why It Works
In a normal Linux system, PID 1 is init or systemd, which handles signal forwarding and reaping zombie processes.
In a container, your application runs as PID 1. Most applications aren’t designed for this, they don’t forward signals to child processes or reap zombies.
The --init flag runs tini (a tiny init system) as PID 1, with your application as its child. Tini:
- Forwards signals properly (so
docker stopworks) - Reaps zombie processes
- Exits with your application’s exit code
If your container spawns child processes-shell scripts, worker pools, anything with child_process-you probably need this.
Spent hours debugging why graceful shutdown wasn’t working. One flag fixed it.