nodejs / docker-node

Official Docker Image for Node.js :whale: :turtle: :rocket:
https://hub.docker.com/_/node/
MIT License
8.25k stars 1.97k forks source link

[Discussion] Node.js in Docker for production #161

Open flowirtz opened 8 years ago

flowirtz commented 8 years ago

So, in the official guide it states that the Dockerfile is not ment for production, however there is just very little information on what a production environment whould actually look like. I am curious now and really want to create a list of differences in here.

So far I've got the following: A production environment

But what else really would there be?

This Issue is more of a discussion, evolved from PR666 and is mostly aimed at @nodejs/docker

Starefossen commented 8 years ago

Hi @FWirtz,

Have you checked out the Node.js Docker Best Practices - the link is kind of hidden in the Docker guide.

flowirtz commented 8 years ago

Hi @Starefossen,

you are right, there are some more Tips that I have not taken into account yet. Thank you! Can you think of something else that would differ in production?

davidbnk commented 8 years ago

I had also a hard time finding good tips on workflow and production recommendations. Some good ones I found:

Some interesting stuff:

http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html (very good) http://anandmanisankar.com/posts/docker-container-nginx-node-redis-example/ https://github.com/veggiemonk/awesome-docker

flowirtz commented 8 years ago

Wow. Thats awesome. Thank you very very much. I didnt have time to read all of it yet, but I will on the weekend. You really give me some interesting insights that I totally did not think of before. Really, thank you very much!

Starefossen commented 8 years ago

Great list @davidbnk! đź‘Ť Feel free to propose a PR to the Node.js Docker Best Practices guide.

davidbnk commented 8 years ago

@FWirtz No problem! @Starefossen Sure, will do

flowirtz commented 8 years ago

@davidbnk Wow david, that list and the articles really are awesome - THANK YOU!!!

Magnitus- commented 8 years ago

"might use node index.js over npm start (Thanks @pesho src )" If you use a lightweight init system like dumb-init, you can use npm start and still have the init system relay SIGTERM signals sent to your container to npm start's child process (ie, your node app).

schmod commented 7 years ago

If you use a lightweight init system like dumb-init, you can use npm start and still have the init system relay SIGTERM signals sent to your container to npm start's child process (ie, your node app).

If your application launches through a shell script, or spawns children via child_process, it's also a very good idea to use a real init system.

In addition to all that, this is a good time to make sure that your application intercepts and handles SIGTERM correctly, and calls process.exit() after performing any necessary cleanup tasks, instead of waiting for docker to timeout and send SIGKILL. Most Node applications do not handle this adequately -- by default, Node ignores SIGTERM.

(On that note, it's also worth remembering that, by default, docker only waits 5 seconds for the process to exit after sending SIGTERM before it sends SIGKILL. This is a very short amount of time, which can be a problem for applications that require more time to exit cleanly.)

qiulang commented 2 years ago

Hi, I tested node14+npm6 & node16+npm 8 I found that npm does NOT swallow SIGTERM signal as many articles said, including Node.js Docker Best Practices guide.

I am also confused with the Pid 1 problem stated in the guide. I asked my question at SO The problem with npm start to start the node app in docker but no answer yet.

So can someone (maybe @Starefossen ) cast some light on it ?

Thanks!

qiulang commented 2 years ago

I found this closed issue lifecycle: propagate SIGTERM to child

So npm did fix it but the latest comment (2017) in that said "Yes, this isn’t working, at least with bash; npm runs its lifecycle processes in a shell, and bash doesn’t forward SIGTERM to its children." But that was 5 years ago!

mahnunchik commented 8 months ago

Fixed in npm@v10.3.0 https://github.com/npm/cli/issues/6684