![]() The image inherits everything in the base, then adds additional content via the. CMD 'app.js' This Dockerfile selects the official Node.js Docker image as its base via the FROM statement. Don't use the shell form of CMDįor reasons talked about in " Docker Shell vs. Here’s a Dockerfile that works for the sample application: FROM node:16 WORKDIR /app COPY package.json. ![]() You might be tricked by npm stopping the node process with CTRL-C in Docker, but that isn't the same as the node process responding to an exit signal gracefully. One piece of advice you'll find in articles suggesting you use an init system is to not use npm start as your Dockerfile CMD, and this still holds true - npm isn't an init system, and it doesn't forward exit signals to the node process, so none of the handlers we wrote above will work. on( 'SIGTERM', shutdown) npm doesn't forward signals Node.js can respond to those signals, but numerous blog posts would have you believe otherwise. For example, a Node.js process running as PID 1 will not respond to SIGINT ( CTRL-C) and similar signals. Node.js was not designed to run as PID 1 which leads to unexpected behaviour when running inside of Docker. The current (Nov 2, 2020) version of the Docker and Node.js best practices has some misinformation: ![]() This can be a problem with the SIGTERM signal in particular, leaving processes running in the background.Īnother reason to have an init system is to prevent orphaned child processes when the main process exits or dies, but this isn't a problem with single-threaded applications like Node.js. ![]() The Dockerfile CMD becomes PID 1 in the running container, and as a result it is treated differently by the kernel when it comes to process signals - if the process hasn't registered a handler for a specific signal, the kernel will ignore the signal entirely rather than falling back to a default behavior. This topic is deep enough to warrant its own post, but here is a short explanation: But you do need to think about how your application handles exit signals.
0 Comments
Leave a Reply. |