Skip to content

Signals and PID 1: why docker stop takes 10 seconds

Select an icon to see what it is.

Step 6 of 6

--init adds a small PID 1

docker run --init starts the container with docker-init, a tiny init program, as PID 1 and your command as its child. docker-init forwards signals to the child and reaps zombie processes. So even a program with no signal handlers can stop on SIGTERM.

$ docker run -d --init --name web nginx
PrevNext

Process tree inside the container

PID 1 /sbin/docker-init -- <your command>
PID 7 \_ <your command>
SIGTERM to PID 1 -> forwarded to PID 7

Try it yourself

Time docker stop on a process that ignores SIGTERM, then fix it with --init.

$ docker run -d --name slow alpine sleep 1000
$ time docker stop slow
$ docker rm slow
$ docker run -d --init --name quick alpine sleep 1000
$ time docker stop quick
$ docker rm quick

Quiz

1. Why can a program ignore SIGTERM as PID 1?
2. Which CMD makes the app PID 1?
3. What does docker run --init add?