49

I am refactoring a couple of node.js services. All of them used to start with forever on virtual servers, if the process crashed they just relaunch.

Now, moving to containerised and state-less application structures, I think the process should exit and the container should be restarted on a failure.

Is that correct? Are there benefits or disadvantages?

Patrick
  • 7,903
  • 11
  • 52
  • 87

3 Answers3

54

My take is do not use an in-container process supervisor (forever, pm2) and instead use docker restart policy via the --restart=always (or one of the other flavors of that option). This is more inline with the overall docker philosophy, and should operate very similarly to in-container process supervision since docker containers start running very quickly.

The strongest advocate for running in-container process supervision I've seen is in the phusion baseimage-docker README if you want to explore the other position on this topic.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • 3
    You lose zero-downtime deployments by using docker, though. With bare-metal PM2, you can have rolling deployments without needing a load balancer. – Nepoxx Jul 14 '15 at 20:42
  • 28
    Uh, only if you deploy into a running container instead of rebuilding a new container with the new app code, which destroys most of the benefit docker aims to deliver. Do zero downtime at the load balancer level. Have more than one instance of your app running. – Peter Lyons Sep 08 '15 at 15:25
  • 1
    Another interesting point on this: if you were using PM2 to start multiple instances as a webserver you will need to have N number of host ports exposed and handle that on the LB rather than PM2 being a pseudo-LB. Lots of abstractions. *sigh* – John Culviner Apr 11 '16 at 16:14
22

While it's a good idea to use --restart=always as a failsafe, container restarting is relatively slow (5+ seconds with the simple Hello World Node server described here), so you can minimize app downtime using something like forever.

A downside of restarting the process within the container is that crash recovery can now happen two ways, which might have implications for your monitoring, etc.

Aidan Feldman
  • 5,205
  • 36
  • 46
  • will using both (forever AND restart) cause some other problem or conflito? I read here (https://docs.docker.com/engine/admin/host_integration/#/using-a-process-manager) that using a process manager with restart policy may not be a good idea. have you ever tried both? – Victor Ferreira Nov 03 '16 at 21:22
  • PM2 has a specific binary to handle docker so it should be fine. See http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/#pm2-docker-helper – Haoliang Yu Jan 01 '17 at 18:37
  • Docker doesn't take 5+ seconds to start/restart, at least not anymore. My containers start in < 1s on my MCB laptop. Still a +1 though – The Onin Jul 17 '21 at 14:40
1

Node needs clustering setup if you are running on a server with multiple CPUs.

With PM2 you get that without writing any extra code. http://pm2.keymetrics.io/docs/usage/cluster-mode/

Unless you are using a bunch of servers with single CPU instances than i would say use PM2 in production.

pm2 will also be quicker to restart than docker

Richard Torcato
  • 2,504
  • 25
  • 26