We have been using pm2 quite successfully for running apps on our servers. We are currently moving to docker and we saw http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
But what is the point of actually using both together? Does not docker provide everything pm2 does?
update:
you may not be in favour of using pm2 inside Docker but sometimes the application requirements are different and you may need to run two nodejs application in one docker container, so in case if you want to run frontend and backend application in the same container then in the case pm2 work well then other workarounds.
So now we have pm2-runtime which run docker process in the foreground, your application will be running the foreground with pm2 andy you can expect the same result as running without pm2.
So with pm2-run time
pm2 restart all
, which will save time development.or If you want to run multiple node application in container then you can process.yml
process.yml file You can also create a Ecosystem file in YAML format. Example:
This will allow the container to run multiple nodejs processed.
If you want to run with Keymetrics.
Disable Auto restart:
With this flag the container will be killed if the nodejs process killed or stoped due error or exception. As sometime we do not auto restart the process but we want to restart container.
Without pm2 runtime
As rule of thumb only one process per container. So keeping this in mind you start your process inside the container using
node start server.js
as you did without docker. What will happens here if nodejs server crash? your container will be killed in this case. which one should avoid doing this.Your container will be killed whenever the nodejs server goes down because the primary process will go down and that process should be in the foreground as being the primary process of the container.
So ultimately there is pm2 for that. This is how you can use pm2 and supervisord together to achieve that.
If you are also looking for an example, Here is the dockerfile and required config file. Using the alpine most lightweight image of 2mb.
supervisord.conf
pm2.conf
Usually there is no point in using pm2 inside of a docker.
Both PM2 and Docker are process managers and they both can do log forwarding, restart crashed workers and many other things. 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. Other docker based environments (like ECS or Kubernetes) can also do it.
2) If you run multiple processes you will make monitoring harder. CPU/Memory metrics are no longer directly available to your enclosing environment.
3) Health checks requests for a single PM2 process will be distributed across workers which is likely to hide unhealthy targets
4) Worker crashes are hidden by pm2. You will hardly ever know about them from your monitoring system (like CloudWatch).
5) Load balancing becomes more complicated since you're virtually going to have multiple levels of load balancing.
Also running multiple processes inside of a docker container contradicts the philosophy of docker to keep a single process per container.
One scenario I can think of is if you have very limited control over your docker environment. In this case running pm2 may be the only option to have control over worker scheduling.