Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 268
Patrick Avatar asked Mar 09 '15 13:03

Patrick


People also ask

Should you use Pm2 in Docker?

If you run pm2 inside of a docker container you will hide potential issues with your service, at least following: 1) If you run a single process per container with pm2 you will not gain much except for increased memory consumption. Restarts can be done with pure docker with a restart policy.

Why should we use Pm2?

Pm2 is a process manager that helps to keep your Node. js application alive all the time. Pm2 runs in the background as a service or a daemon, managing your Node. js application for you.

Should you run multiple services in Docker container?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.


2 Answers

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.

like image 107
Peter Lyons Avatar answered Sep 24 '22 01:09

Peter Lyons


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.

like image 23
Aidan Feldman Avatar answered Sep 24 '22 01:09

Aidan Feldman