keymetrics / pm2-plus-tech-questions

http://docs.keymetrics.io/docs/pages/faq-troubleshooting/
27 stars 3 forks source link

pm2-runtime doesn't run as node user in Docker #315

Open kevinvugts opened 9 months ago

kevinvugts commented 9 months ago

I am trying to run pm2-runtime not as the root user since this is bad practice in terms of security. Below is my Dockerfile. However, when pushed to Azure App services the pm2-runtime never runs. There is also no log/error code or anything. The container in azure just stops booting after 230 seconds because it couldn't ping my port obviously.

Here is the file. Can someone help me identify the issue? Perhabs @Unitech could elaborate more on this since I am out of options after days of debugging and trial and error.


FROM --platform=linux/amd64 node:18-alpine as build
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev vips-dev git > /dev/null 2>&1
ENV NODE_ENV=production

# Set working directory to /opt/ and copy package.json an yarn.lock in this folder
WORKDIR /opt/
COPY package.json yarn.lock ./
RUN yarn global add node-gyp
RUN yarn config set network-timeout 600000 -g && yarn install --production
ENV PATH /opt/node_modules/.bin:$PATH

# Set working directory to /op/app and copy all Strapi related files in this folder
WORKDIR /opt/app
COPY . .
#RUN yarn build -> we skip building for now since we do not want to serve the admin panel on production

# Creating the final production image
FROM --platform=linux/amd64 node:18-alpine
RUN apk add --no-cache vips-dev
ENV NODE_ENV=production

WORKDIR /opt/
COPY --from=build /opt/node_modules ./node_modules
WORKDIR /opt/app
COPY --from=build /opt/app ./

RUN chown -R node:node /opt/app
RUN chown -R node:node /opt/node_modules
USER node

RUN yarn global add pm2
ENV PM2_PUBLIC_KEY xxxxxxxxxxxxx [hidden for security]
ENV PM2_SECRET_KEY xxxxxxxxxxxx [hidden for security]

# After installing pm2 globally
RUN echo "Global bin directory: $(yarn global bin)"
ENV PATH /home/node/.config/yarn/global/node_modules/.bin:$PATH
RUN mkdir -p /home/node/.config/yarn/global && chown -R node:node /home/node/.config
ENV PATH="/home/node/.config/yarn/global/node_modules/.bin:${PATH}"

USER root 
# RUN chown -R node:node /home/node/.config/yarn/global/node_modules/.bin/pm2
ENV PATH /opt/node_modules/.bin:$PATH
# RUN chown -R node:node /opt/app/node_modules/.bin/pm2
USER node
EXPOSE 1337

# CMD ls -lah /opt/app && ls -lah /opt/node_modules/.bin && node start.js
CMD ["pm2-runtime", "start.js"]```