This repository exists to prove that you need an init
process in your Docker container.
Uses Node.js as an example, but this is basically applicable to all runtimes.
You can read more about PID 1, orphaned processes, zombie processes, process adopting in my blog article.
Runs the docker container without the --init
flag to prove that the Node.js runtime doesn't take care of zombie processes.
./run.sh
Runs the docker container with the --init
flag to prove that the init
process takes care of zombie processes.
./run.sh with-init
- Build Node.js application image
- Run image as a container. Boots up the
main.js
script which starts a child process that runschild.js
. The child process starts another child process that runssub-child.js
. - Print out the process table before killing any processes.
- Kill
child.js
process. Thussub-child.js
becomes an orphan and PID 1 (main.js
process orinit
) adopts it. - Kill
sub-child.js
process. Thussub-child.js
is terminated and becomes a zombie process. In case ofinit
, this zombie process is removed from the process table.
This is the output of running the bash script without the with-init
argument. This shows that there is a zombie process at the end.
$ ./run.sh
[+] Building 1.7s (10/10) FINISHED
....
Currently running processes (before kills):
UID PID PPID C STIME TTY STAT TIME CMD
root 1 0 5 19:25 ? Ssl 0:00 node lib/main.js
root 14 1 0 19:26 ? Sl 0:00 /usr/local/bin/node /app/lib/child.js
root 21 14 0 19:26 ? Sl 0:00 /usr/local/bin/node /app/lib/sub-child.js
root 28 0 0 19:26 pts/0 Rs+ 0:00 ps -eaf -w 10000
Killed child process
Currently running processes (after killing child):
UID PID PPID C STIME TTY STAT TIME CMD
root 1 0 2 19:25 ? Ssl 0:00 node lib/main.js
root 21 1 3 19:26 ? Sl 0:00 /usr/local/bin/node /app/lib/sub-child.js
root 46 0 0 19:26 pts/0 Rs+ 0:00 ps -eaf -w 10000
Killed sub-child process
Currently running processes (after killing sub-child):
UID PID PPID C STIME TTY STAT TIME CMD
root 1 0 1 19:25 ? Ssl 0:00 node lib/main.js
root 21 1 1 19:26 ? Z 0:00 [node] <defunct>
root 64 0 0 19:26 pts/0 Rs+ 0:00 ps -eaf -w 10000
Found 1 zombie processes!