Home > database >  Why Docker Image getting so large for node js app
Why Docker Image getting so large for node js app

Time:09-16

here is the screenshoot of my Dockerfile: enter image description here

And this is my Docker image file: enter image description here

why I getting large docker image using this Dockerfile command eventhough I have add the .dockerignore to ignore node modules, is there any way to solve this image size ? Can we use webpack to bundle and implement tree shaking and make the size smaller for prodction

CodePudding user response:

  1. To reduce the docker image size, use the alpine tag. this tag uses Alpine Linux as a base image, and this will decrease your built image. (What is Alpine Linux? Alpine Linux is a security-oriented, lightweight Linux distribution based on musl, libc, and busybox.)

  2. you can use multi-stage builds to build your docker image.

  3. use npm prune --production, this command will remove the packages specified in your devDependencies.

A sample Dockerfile that follows the mentioned conditions:

FROM node:14-alpine as build

WORKDIR /opt/app

COPY . /opt/app

RUN npm ci \
    && npm run build \
    && npm prune --production


FROM node:14-alpine

ENV NODE_ENV=production

USER node
WORKDIR /opt/app

COPY --from=build /opt/app/package*.json /opt/app/
COPY --from=build /opt/app/node_modules/ /opt/app/node_modules/
COPY --from=build /opt/app/dist/ /opt/app/dist/

EXPOSE 3000

CMD ["npm", "run", "start:prod"]

CodePudding user response:

you can use alpine docker image like node:14.5.0-alpine

its recommended using alpine images as it's speed, secure, and lightweight

  • Related