Skip to content

Instantly share code, notes, and snippets.

@micalevisk
Last active May 12, 2024 04:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save micalevisk/9ee47135b22f284dd73de6ea6d5aa864 to your computer and use it in GitHub Desktop.
Save micalevisk/9ee47135b22f284dd73de6ea6d5aa864 to your computer and use it in GitHub Desktop.
Production Dockerfile example for NestJS projects.

Let's say you have the following project:

.
├── Dockerfile
├── .dockerignore
├── nest-cli.json
├── package.json
├── package-lock.json
├── README.md
├── src
│   ├── app.controller.ts
│   ├── app.module.ts
│   ├── app.service.ts
│   └── main.ts
├── tsconfig.build.json
└── tsconfig.json

Make sure the main entry of your package.json points to the transpiled code like "main": "./dist/main.js"

You could use that Dockerfile to build the image like this:

docker build . --tag  "my-app:latest" --build-arg PORT=3000
## ===========================================================> The common stage
FROM node:16.14 AS base
ENV NODE_ENV=production
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
## Remove unnecessary files from `node_modules` directory
RUN ( wget -q -O /dev/stdout https://gobinaries.com/tj/node-prune | sh ) \
&& node-prune
## ======================================================> The build image stage
FROM base AS build
ENV NODE_ENV=development
COPY . .
## This step could install only the missing dependencies (ie., development deps ones)
## but there's no way to do that with this NPM version
RUN npm ci
## Compile the TypeScript source code
RUN npm run build
## =================================================> The production image stage
FROM node:16.14-alpine3.14 AS prod
ENV NODE_ENV=production
ARG PORT=3000
ENV PORT=$PORT
EXPOSE $PORT
HEALTHCHECK --interval=10m --timeout=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:$PORT || exit 1
WORKDIR /app
## Copy required file to run the production application
COPY --from=base --chown=node:node /app/node_modules ./node_modules
COPY --from=base --chown=node:node /app/*.json ./
COPY --from=build --chown=node:node /app/dist ./dist/
## https://engineeringblog.yelp.com/2016/01/dumb-init-an-init-for-docker.html
RUN apk add --no-cache dumb-init
## Dropping privileges
USER node
## Running the app wrapped by the init system for helping on graceful shutdowns
CMD ["dumb-init", "node", "."]
## ===========================================================> The common stage
FROM node:16.14 AS base
ENV NODE_ENV=production
WORKDIR /app
COPY package.json \
yarn.lock \
./
RUN yarn --frozen-lockfile --ignore-scripts --production \
&& yarn cache clean
## Remove unnecessary files from `node_modules` directory
RUN ( wget -q -O /dev/stdout https://gobinaries.com/tj/node-prune | sh ) \
&& node-prune
## ======================================================> The build image stage
FROM base AS build
ENV NODE_ENV=development
COPY . .
## This step could install only the missing dependencies (ie., development deps ones)
## but there's no way to do that with this NPM version
RUN yarn --frozen-lockfile --ignore-scripts
## Compile the TypeScript source code
RUN yarn build
## =================================================> The production image stage
FROM node:16.14-alpine3.14 AS prod
ENV NODE_ENV=production
ARG PORT=3000
ENV PORT=$PORT
EXPOSE $PORT
HEALTHCHECK --interval=10m --timeout=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:$PORT || exit 1
WORKDIR /app
## Copy required file to run the production application
COPY --from=base --chown=node:node /app/node_modules ./node_modules
COPY --from=base --chown=node:node /app/*.json ./
COPY --from=build --chown=node:node /app/dist ./dist/
## https://engineeringblog.yelp.com/2016/01/dumb-init-an-init-for-docker.html
RUN apk add --no-cache dumb-init
## Dropping privileges
USER node
## Running the app wrapped by the init system for helping on graceful shutdowns
CMD ["dumb-init", "node", "."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment