I've written a Python program which ensures that its threads are gracefully stopped upon receiving a Unix SIGTERM signal. I understand that docker stop sends a SIGTERM request to the main process inside the container, and after a grace period, SIGKILL. The documentation for docker-compose down states that it "stops containers", but does not say explicitly how. I assume that it calls docker stop
on each container?
Following the links given by user2915097, it seems that this is indeed the case. The down
method of the Project
class invokes its stop
method, which in terms seems to iterate over the containers and call stop
on each one.
NPM and Dockerfile ENTRYPOINT: Just Don't
I'm going to add this here so it is documented somewhere that some one else finds this answer too.
Node.JS Express + MongoDB app using Docker Compose. Pretty basic BUT it does not trigger the SIGINT
or the SIGTERM
events.
https://github.com/neozenith/hello-mongo/tree/4c1c3d9a55b574ba5a1c5f45dbf708c65a70c434
Solution
https://github.com/neozenith/hello-mongo/commit/c7e1b7497f204ffaa9387f99cb88188dadc1c094
BEFORE:
# WAS THIS
CMD [ "npm", "start" ]
AFTER:
# FUN FACT: DO NOT RUN ["npm", "start"] as the ENTRYPOINT
# It does not forward the SIGTERM and SIGINT events to Node
ENTRYPOINT [ "node", "server.js" ]
The exec form of ENTRYPOINT
sends through the SIGTERM
and SIGINT
to PID 1.
https://docs.docker.com/engine/reference/builder/#exec-form-entrypoint-example
The key here is that npm
does not forward that to node
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With