0

I want to create a docker container. I've written a Docker file like this

FROM node:12-buster

WORKDIR /opt/myapps/
COPY . /opt/myapps/

RUN apt update
RUN apt upgrade -y
RUN apt install git -y

RUN git config --global user.email "user@git.com"
RUN git config --global user.name "user"
RUN npm install -g pm2
RUN pm2-runtime main.js
RUN pm2 save
RUN chmod +x /opt/myapps/entrypoint.sh

EXPOSE 4001

CMD ["./entrypoint.sh"]

and this is my entrypoint.sh

#!/bin/bash

#node thing
npm install
#npm start
pm2-runtime main.js --exp-backoff-restart-delay=100

when I do a build it will definitely get stuck in the final process like this

Step 10/14 : RUN pm2-runtime main.js
 ---> Running in 6061a8236379
2021-03-09T12:38:20: PM2 log: Launching in no daemon mode
2021-03-09T12:38:20: PM2 log: App [main:0] starting in -fork mode-
2021-03-09T12:38:20: PM2 log: App [main:0] online
Server started on port :4001

the process never continued when it got there. Does anyone know why this happened? I'm quite new to the docker world

  • Do you actually need pm2; can you delete everything between `COPY` and `EXPOSE`, and just set `CMD ["node", "main.js"]`? Docker on its own can provide restarts on failure, log management, _etc._ See also [what is the point of using pm2 and docker together?](https://stackoverflow.com/questions/51191378/what-is-the-point-of-using-pm2-and-docker-together) – David Maze Mar 09 '21 at 13:50

1 Answers1

0

A RUN step in docker executes the command, waits for the command to exit, and captures the changes to the container filesystem as a new layer in the image. Therefore docker will wait until the pm2 start command exits, which it doesn't appear it will exit since it's a service listening on a port for requests.

For running a service, you typically want this run as the container's entrypoint to access the service when the container is running, rather than starting it during the image build.

At a high level, you want to separate things into three buckets:

  1. the binaries and library installs for the application, these go in the image created with a Dockerfile
  2. the persistent data, this goes in a volume
  3. the configuration, this goes in the compose file, kubernetes manifest, environment variables, or injected as a file in a volume
BMitch
  • 231,797
  • 42
  • 475
  • 450