Reputation: 441
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?
Upvotes: 0
Views: 963
Reputation: 158847
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.
FROM node:12-alpine
# Note that the Dockerfile already puts us in the right directory
WORKDIR /TelegramChannelMonitor
...
# Important: no ENTRYPOINT
# (There can be a default CMD if you think one path is more likely)
Then when you want to run this application, run two containers, and in each, make the main container command run a different script.
docker build -t telegram-channel-monitor .
docker run -d -p 8080:8080 --name kick-non-premium \
telegram-channel-monitor \
node services/kickNonPremium.js
docker run -d -p 8081:8080 --name monitor-joined \
telegram-channel-monitor \
node services/monitorTelegramJoinee.js
You can have a similar setup using Docker Compose. Set all of the containers to build: .
, but set a different command:
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 plain node
instead of pm2
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?.)
Upvotes: 4
Reputation: 3327
Try pm2 ecosystem for apps(i.e services) declaration and run pm2
in non-backrgound mode or pm2-runtime
https://pm2.keymetrics.io/docs/usage/application-declaration/ https://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
Upvotes: 0