Should I use forever/pm2 within a (Docker) container?
Asked Answered
R

3

50

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?

Risa answered 9/3, 2015 at 13:0 Comment(0)
S
56

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.

Selemas answered 9/3, 2015 at 14:59 Comment(4)
You lose zero-downtime deployments by using docker, though. With bare-metal PM2, you can have rolling deployments without needing a load balancer.Disused
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.Selemas
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. sighSelfdeprecating
@PeterLyons, I was trying to containerize an express api with forever, running into issues, thanks to this answer I see the error of my ways.Incongruous
B
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.

Bronnie answered 27/11, 2015 at 6:21 Comment(3)
will using both (forever AND restart) cause some other problem or conflito? I read here (docs.docker.com/engine/admin/host_integration/#/…) that using a process manager with restart policy may not be a good idea. have you ever tried both?Pensile
PM2 has a specific binary to handle docker so it should be fine. See pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/…Anonymous
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 thoughGodship
E
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

Eminence answered 20/9, 2018 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.