I am working on telegram group for premium members in which I have two services, one is monitoring all the joinee and other one is monitoring if any member has expired premium plan so It would kick out that user from the channel. I am very very new to Docker and deployment things. So I am very confused that, to run two processes simultaneously with one Dockerfile. I have tried like this.
start.sh
#!/bin/bash
cd TelegramChannelMonitor
pm2 start services/kickNonPremium.js --name KICK_NONPREMIUM
pm2 start services/monitorTelegramJoinee.js --name MONITOR_JOINEE
Dockerfile
FROM node:12-alpine
WORKDIR ./TelegramChannelMonitor
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
ENTRYPOINT ["/start.sh"]
What should I do to achieve this?
2
Answers
Try pm2 ecosystem for apps(i.e services) declaration and run
pm2
in non-backrgound mode orpm2-runtime
https://pm2.keymetrics.io/docs/usage/application-declaration/
https://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
A Docker container only runs one process. On the other hand, you can run arbitrarily many containers off of a single image, each with a different command. So the approach I’d take here is to build a single image; as you’ve shown it, except without the
ENTRYPOINT
line.Then when you want to run this application, run two containers, and in each, make the main container command run a different script.
You can have a similar setup using Docker Compose. Set all of the containers to
build: .
, but set a differentcommand:
for each.(The reason to avoid
ENTRYPOINT
here is because the syntax to override the command gets very clumsy: you need--entrypoint node
before the image name, but then the rest of the arguments after it. I’ve also used plainnode
instead ofpm2
since a Docker container provides most of the functionality of a process supervisor; see also what is the point of using pm2 and docker together?.)