1

I have written a multi-stage dockerfile with pm-2 docker, but when I am trying to run pod with my image, pod crashes,

Dockerfile

FROM node:16.15.1-alpine3.16 as build

ENV NODE_TLS_REJECT_UNAUTHORIZED=0

# Create app directory
WORKDIR /app

ENV NODE_OPTIONS --max-old-space-size=4096

# Install app dependencies
COPY package.json .

RUN apk update && apk add vim jq busybox-extras

RUN npm install pm2 -g 

RUN npm install --save-exact

COPY . .

FROM node:16.15.1-alpine3.16 as main

COPY --from=build /app /

CMD ["pm2-docker", "index.js"]

Error

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/pm2-docker'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

multi-stage dockerfile written is correct?

SVD
  • 385
  • 4
  • 24
  • you don't have pm2 installed in main. You have to run npm install pm2 again in main. You have to set a WORKDIR in main too (before the COPY). – Dr Claw Jul 01 '22 at 14:03
  • Docker itself provides a lot of the same functionality that pm2 provides, like restarting the application on failure; can you just set `CMD ["node", "index.js"]` without pm2? Also see [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 Jul 01 '22 at 14:03

1 Answers1

0

I just faced this issue and like @Dr Claw said, adding npm install -g pm2 in the main image after COPY did the trick. I removed npm install -g pm2 from the build image

Chet
  • 134
  • 1
  • 11