cam
cam

Reputation: 149

Docker container and PM2 runnng in ec2 instance

I have an ec2 instances that is running a node application. I am thinking of doing a container implementation using docker. The pm2 is running two application one is the actual node application (express and pug) and a cronjob using agenda. Is it a good idea to put my applications in one container?

I am not yet familiar with the pros and cons of this and I read that docker is already a process manager. How will the pm2 fit in all of this once I implement it. Or should I just ditch docker and run the applications in the native linux of my ec2.

Upvotes: 2

Views: 2932

Answers (1)

Robert-Jan Kuyper
Robert-Jan Kuyper

Reputation: 3346

You have a couple of questions, I try to answer them below:

1. Is it a good idea to put my applications in one container?

It depends, there are many cases why you would like to run the same container doing multiple things. But it really depends on the CPU/RAM/Memory usage of the job. And how often does it run?

Anyway from experience I can say if I run a cronjob from the same container, I would always use a worker approach for this using either NodeJS cores worker_threads or cluster module. Because you do not want that a cronjob impacts the behavior of the main thread. I have an example of running 2 applications on multiple threads in the following repo.

2. should I just ditch docker and run the applications in the native linux of my ec2

Docker and PM2 are 2 really different things. Docker is basically to containerize your entire Node app, so it is much easier to ship it. PM2 is a process manager for node and makes sure your app is up and comes with some nice metrics and logs UI on PM2 metrics. You can definitely use the 2 together, as PM2 makes also sure your app will start up after it crashes.

However, if you use pm2 you have to use the pm2-runtime when using docker. Example Dockerfile:

FROM node:16.9.0

WORKDIR /home/usr/app

COPY . .

RUN npm ci && npm run build

# default command is starting the server
CMD ["npx", "pm2-runtime", "npm", "--", "start"]

Upvotes: 3

Related Questions