Docker multi stage optimization causes an error with node.js

53 views Asked by At

Hello I'm working with my first docker project but having a problem reducing the image size.

FROM node:14.15.0-alpine

RUN mkdir /app

WORKDIR /app

COPY . .
# COPY ./medichis-kubernetes/config.json .

RUN npm install && npm run build

EXPOSE 7575

CMD [ "node", "./dist/src/bin/www.js" ]

The single stage docker image with this code is run okay but, the multi stage docker image with the following codes causes an error.

# Stage 1: Build Stage
FROM node:14.15.0-alpine AS BUILD_IMAGE
WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

RUN npm run build

# Stage 2: Production Stage
FROM node:14.15.0-alpine
WORKDIR /app

COPY --from=BUILD_IMAGE /app/dist ./dist
COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
COPY --from=BUILD_IMAGE /app/package*.json ./
COPY --from=BUILD_IMAGE /app/config.json ./

EXPOSE 7575

CMD [ "node", "./dist/src/bin/www.js" ]

(node:1) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, scandir './src/modules' (Use node --trace-warnings ...to show where the warning was created) (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:1) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open './.banner' (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)

  1. I checked the image is created right with a tag.
  2. I built npm project locally, and the directory is Root/dist/src/bin/www.js that is same as the one inside the dockerfile.

What do you think I can do more? Thank you in advance.

0

There are 0 answers