Docker's --init Flag Fixes Zombie Processes

· 1 min read
docker containers debugging

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 stop works)
  • 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.

Available for rescue and re-platforming work

I take over platforms that already exist and are in trouble. Node, TypeScript, React and Laravel, mostly in regulated or high-traffic environments. If something needs rescuing, re-platforming or finishing, my full history is on the CV.

Related Posts

Comments