Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS minimize dockerfile

I want to dockerize my nestjs api. With the config listed below, the image gets 319MB big. What would be a more simple way to reduce the image size, than multi staging?

Dockerfile

FROM node:12.13-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
CMD npm start

.dockerignore

.git
.gitignore
node_modules/
dist/
like image 363
dewey Avatar asked Apr 07 '20 09:04

dewey


People also ask

Why is my Docker image so large?

A Docker image takes up more space with every layer you add to it. Therefore, the more layers you have, the more space the image requires. Each RUN instruction in a Dockerfile adds a new layer to your image. That is why you should try to do file manipulation inside a single RUN command.


1 Answers

For reducing docker image size you can use

  1. Multi-stage build
  2. Npm prune

While using multi-stage build you should have 2(or more) FROM directives, as usual, the first stage does build, and the second stage just copies build from the first temporary layer and have instructions for run the app. In our case, we should copy dist & node_modules directories.

The second important moment its correctly split dependencies between 'devDependencies' & 'dependencies' in your package.json file.

After you install deps in the first stage, you should use npm prune --production for remove devDependencies from node modules.

FROM node:12.14.1-alpine AS build


WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . ./

RUN npm run build && npm prune --production


FROM node:12.14.1-alpine

WORKDIR /app
ENV NODE_ENV=production

COPY --from=build /app/dist /app/dist
COPY --from=build /app/node_modules /app/node_modules

EXPOSE 3000
ENTRYPOINT [ "node" ]
CMD [ "dist/main.js" ]

If you have troubles with node-gyp or just want to see - a full example with comments in this gist:

https://gist.github.com/nzvtrk/cba2970b1df9091b520811e521d9bd44

More useful references:

https://docs.docker.com/develop/develop-images/multistage-build/

https://docs.npmjs.com/cli/prune

like image 85
Artur Khrabrov Avatar answered Nov 09 '22 21:11

Artur Khrabrov