Fixing the PID1 problem within a dockerised Node.js app, AKA untrap the untrappable

In case if some of you are in the same situation: you want to use Node.js in Docker as a simple development environment. You want to run your apps or commands with it from the host, but you doesn’t like that Node taken PID1 and SIGINT, SIGKILL or SIGTERM not working (like CTRL+C).

Here is the solution: Give PID1 for something else!

First, modify the ENTRYPOINT at the end of the Dockerfile for the following or not include anything at all (the default entrypoint is /bin/sh -c in Docker):

ENTRYPOINT [“/bin/bash”, “-c”]

Than I run with this shell script, which has the filename of node and have chmod +x:

#!/bin/bash
docker run — rm -it -p 8083:80 -v $HOME/node/work/:/root/node/:rw node_node “echo pid1 > /dev/null && node $@”

node_node is my image’s name which has Node.js installed. We run with --rm because this is a dev environment. If the app is exited, we doesn’t need to manually delete the container.

The trick is ”echo pid1 > /dev/null && node $@” which is the command. $@ is a shell script to accept user input from command line. The double-quotes making sure that /bin/bash will execute it as a whole.

echo will trap PID1 and sending output to /dev/null.

For example ./node -v will return the version of Node.js inside the running container.

Here is my Dockerised Node.js development environment: https://github.com/DJviolin/Node.js-Development-Environment