here is the screenshoot of my Dockerfile:
And this is my Docker image file:
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:
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.)you can use multi-stage builds to build your docker image.
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