This repository has been archived on 2024-06-15. You can view files and clone it, but cannot push or open issues or pull requests.
snippets/nestjs.Dockerfile

63 lines
1.7 KiB
Docker
Raw Normal View History

2023-04-18 14:31:39 +00:00
###############################
# BUILD FOR LOCAL DEVELOPMENT #
###############################
FROM node:18-alpine As development
# Create app directory
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./
# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci
# Bundle app source
COPY --chown=node:node . .
# Use the node user from the image (instead of the root user)
USER node
########################
# BUILD FOR PRODUCTION #
########################
FROM node:18-alpine As build
WORKDIR /usr/src/app
COPY --chown=node:node package*.json ./
2023-04-18 14:32:13 +00:00
# In the previous development stage we ran `npm ci` which installed all dependencies
# so we can copy over the node_modules directory from the development image
2023-04-18 14:31:39 +00:00
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules
COPY --chown=node:node . .
# Run the build command which creates the production bundle
RUN npm run build
# Set NODE_ENV environment variable
ENV NODE_ENV production
# Passing in --omit=dev ensures that only the production dependencies are installed.
RUN npm ci --omit=dev && npm cache clean --force
USER node
2023-04-18 14:32:13 +00:00
##############
# PRODUCTION #
##############
2023-04-18 14:31:39 +00:00
FROM node:18-alpine As production
# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
# Start the server using the production build
CMD [ "node", "dist/main.js" ]