This Dockerfile is used to build a clean and lightweight image of your NestJS application. please follow the instruction to understand what is happening clearly.
There is a NestJS example project that you can use and build the image after cloning the repository too.
First, make a clone from this repository using:
git clone https://github.com/devopshobbies/docker-templates.git
Build your image using the command:
docker build -t nestjs-temp docker-templates/01-JavaScript-Typescript-Backend/frameworks/nestjs
FROM node:16-alpine as builder
This line sets the NODE_ENV environment variable to build. This tells Node.js that we are in a build environment, which can be used to optimize the build process.
ENV NODE_ENV build
These lines set the user to node (a non-root user), create a working directory at /home/node, and copy the package.json
and package-lock.json
files to the working directory. The npm ci
command installs the project's dependencies based on the package-lock.json
file.
USER node
WORKDIR /home/node
COPY package\*.json ./
RUN npm ci
COPY --chown=node:node . .
RUN npm run build \
&& npm prune --production
Also, if you are using yarn
package manager instead of npm
, likely to change this part to:
USER node
WORKDIR /home/node
COPY package.json ./
# You have to copy the yarn.lock file
COPY yarn.lock ./
RUN yarn install --immutable --immutable-cache --check-cache
COPY --chown=node:node . .
RUN yarn build \
&& yarn install --production --ignore-scripts --prefer-offline
FROM node:16-alpine
ENV NODE_ENV production
These lines copy the package.json
and package-lock.json
files, the node_modules directory, and the dist
directory from the builder stage to the current stage. The --from=builder
flag specifies that the files should be copied from the builder stage, and the --chown=node:node
flag sets the ownership of the copied files to the node user.
USER node
WORKDIR /home/node
COPY --from=builder --chown=node:node /home/node/package\*.json ./
COPY --from=builder --chown=node:node /home/node/node_modules/ ./node_modules/
COPY --from=builder --chown=node:node /home/node/dist/ ./dist/
If you are using yarn
, add this extra line to copy the yarn.lock
file.
COPY --from=builder --chown=node:node /home/node/yarn.lock ./
CMD ["node", "dist/main.js"]