3 ms·
A very common mistake I see (though not related to image size perse) when running Node apps is to do CMD ["npm", "run", "start"]. This is first memory wasteful,
by nodesocket 5y ago
A very common mistake I see (though not related to image size perse) when running Node apps is to do CMD ["npm", "run", "start"]. This is first memory wasteful, as npm is running as the parent process and forking node to run the main script. Also, the bigger problem is that the npm process does not send signals down to its child thus SIGINT and SIGTERM are not passed from npm into node which means your server may not be gracefully closing connections.
- Ramiro 5y agoI never really thought about this, it's a good point. What do you suggest it's used instead of ["npm", "run", "start"]?
- bravetraveler 5y agoI'm not a Node/NPM person, but I imagine they had in mind the equivalent of whatever is expected from npm. I expect some nodejs command to invoke the service directly Edit: Consequently this should make the container logs a bit more useful, beyond better signal handling/respect
- davidjfelix 5y ago["node", "/path/to/your/entrypoint.js"]
- pineconewarrior 5y agoI assume it'd be better to execute index.js directly with node
- nicholasjarnold 5y agoThis is a great use case for tini[0]. Try this, after installing the tini binary to /sbin: ENTRYPOINT ["/sbin/tini", "--"] CMD ["node", "/path/to/main/process.js"] [0]: https://github.com/krallin/tini https://github.com/krallin/tini edit: formatting, sorry.
- remram 5y agoI think this is built into docker now: https://docs.docker.com/engine/reference/run/#specify-an-init-process https://docs.docker.com/engine/reference/run/#specify-an-ini... If you use Kubernetes then you have to add tini for now (https://github.com/kubernetes/kubernetes/issues/84210 https://github.com/kubernetes/kubernetes/issues/84210)
- j1elo 5y agoNode.js has both a Best Practices [0] and a tutorial [1] that instruct to use CMD ["node", "main.js"]. In short: do not run NPM as main process; instead, run Node directly. This way, the Node process itself will run as PID 1 of the container (instead of just being a child process of NPM). The same can be found in other collections of best practices such as [2]. What I do is a bit more complex: an entrypoint.sh which ends up running exec node main.js "$*" Docs then tell users to use "docker run --init"; this flag will tell Docker to use the Tini minimal init system as PID 1, which handles system SIGnals appropriately. [0]: https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md https://github.com/nodejs/docker-node/blob/main/docs/BestPra... [1]: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ [2]: https://dev.to/nodepractices/docker-best-practices-with-node-js-4ln4 https://dev.to/nodepractices/docker-best-practices-with-node... Edit: corrected the part about using --init for proper handling of signals.