SIGTERM does not reach the node script when the docker launches it using `/ bin / sh -c`

When my Docker file ends in

CMD node .

docker starts this container with a command /bin/sh -c "node ."instead of just node .(I know, I could do it with CMD ["node", "."]).

I thought this behavior was really nice, as it means PID1there is /bin/sh, rather than my modest node script inside the container .

If I understand correctly, Iโ€™m PID1responsible for reaping the orphan processes of zombies, and I really donโ€™t answer for it ... So if I /bin/shcan do this, that would be good. (I really thought that was the reason why docker was rewriting mine CMD).

The problem is that when I send SIGTERMto the container (starting with /bin/sh -c "node ."), either through docker-composer stop, or docker-composer kill -s SIGTERM, the signal does not reach my process node, and therefore it gets forcibly killed every time using SIGKILLafter a 10-second grace period. Not nice.

Is there a way for someone to control my zombies and for my node instance to receive signals sent by docker?

+4
source share
3 answers

There are tools designed to solve this problem:

, , , , , bash .

["node", "."], https://nodejs.org/api/process.html#process_signal_events SIGTERM. , .

bash script, trap "exit 0" TERM

, http://skarnet.org/software/s6/

+3

, ENTRYPOINT CMD ENTRYPOINT (exec form) Dockerfile.

ENTRYPOINT, , Docker. , , . /bin/bash -c. , , CMD, ENTRYPOINT .

, ENTRYPOINT Dockerfile, /bin/bash -c {your_command_in_CMD}, , , .

ENTRYPOINT : exec

  • exec form: ENTRYPOINT [ " ", "param1", "param2" ]
  • : param1 param2

Docker: exec, , /bin/bash -c, :

CMD run, , ENTRYPOINT /bin/sh -c, . , PID 1 - Unix - SIGTERM docker stop <container>.

+5

PID1:

Docker :

ENTRYPOINT ["/bin/bash", "-c"]

ENTRYPOINT Docker. /bin/sh -c.

script, node 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 $@"

"echo pid1 > /dev/null && node $@", . $@ script, .

echo PID1 /dev/null.

./node -v Node.js .

- ./node /root/node/hello-world.js, CTRL + C .

- Node.js.

EDIT:

, node bash script $PATH. node -v ./node -v. node Docker, , .:)

0
source

Source: https://habr.com/ru/post/1621513/


All Articles