Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

Open
FWirtz opened this Issue Apr 23, 2016 · 9 comments

Comments

Projects
None yet
5 participants

FWirtz commented Apr 23, 2016

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

Member

Starefossen commented Apr 25, 2016

Hi @FWirtz,

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

FWirtz commented Apr 25, 2016 edited

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?

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

  • You can use the same node_modules for dev and production as long as there is no native modules to be compiled
  • Don't npm install when running the image because npm can be offline and your deploy will fail
  • Make good use of layer caching putting npm install in the image before your code
  • Separate your modules in package.json in "dependencies" and "devDependencies", this way you can run npm install --production to skip devDepencencies
  • Use npm shrinkwrap to ensure that the same versions are installed
  • Don't run node as root
  • Use explicit versions of docker images and node modules whenever possible
  • Use env variables to store sensitive information (see also http://12factor.net/)

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

FWirtz commented Apr 25, 2016

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!

David Benko notifications@github.com schrieb am Mo., 25. Apr. 2016 17:04:

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

  • You can use the same node_modules for dev and production as long as
    there is no native modules to be compiled
  • Don't npm install when running the image because npm can be offline
    and your deploy will fail
  • Make good use of layer caching putting npm install in the image
    before your code
  • Separate your modules in package.json in "dependencies" and
    "devDependencies", this way you can run npm install --production to skip
    devDepencencies
  • Use npm shrinkwrap to ensure that the same versions are installed
  • Don't run node as root
  • Use explicit versions of docker images and node modules whenever
    possible
  • Use env variables to store sensitive information (see also
    http://12factor.net/)

Some interesting stuff:

http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html
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


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#161 (comment)

Member

Starefossen commented Apr 26, 2016 edited

Great list @davidbnk! 👍 Feel free to propose a PR to the Node.js Docker Best Practices guide.

@FWirtz No problem!
@Starefossen Sure, will do

FWirtz commented May 1, 2016

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

@Starefossen Starefossen self-assigned this May 1, 2016

Magnitus- commented May 11, 2016 edited

"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 Jan 25, 2017 edited

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment