2022-12-23 15:36:23 +00:00
|
|
|
###################
|
|
|
|
# BUILD FOR LOCAL DEVELOPMENT
|
|
|
|
###################
|
|
|
|
|
2024-01-22 15:57:44 +00:00
|
|
|
FROM docker.io/node:lts-hydrogen As development
|
2022-12-23 15:36:23 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
###################
|
|
|
|
|
2024-01-22 15:57:44 +00:00
|
|
|
FROM docker.io/node:lts-hydrogen As build
|
2022-12-23 15:36:23 +00:00
|
|
|
|
|
|
|
WORKDIR /usr/src/app
|
|
|
|
|
|
|
|
COPY --chown=node:node package*.json ./
|
|
|
|
|
|
|
|
# In order to run `npm run build` we need access to the Nest CLI.
|
|
|
|
# The Nest CLI is a dev dependency,
|
|
|
|
# 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 into this build image.
|
|
|
|
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
|
|
|
|
|
|
|
|
# Running `npm ci` removes the existing node_modules directory.
|
2023-01-30 15:51:44 +00:00
|
|
|
# Passing in --omit=dev ensures that only the production dependencies are installed.
|
2022-12-23 15:36:23 +00:00
|
|
|
# This ensures that the node_modules directory is as optimized as possible.
|
2023-01-30 15:51:44 +00:00
|
|
|
RUN npm ci --omit=dev && npm cache clean --force
|
2022-12-23 15:36:23 +00:00
|
|
|
|
|
|
|
USER node
|
|
|
|
|
|
|
|
###################
|
|
|
|
# PRODUCTION
|
|
|
|
###################
|
|
|
|
|
2024-01-22 15:57:44 +00:00
|
|
|
FROM docker.io/node:lts-hydrogen As production
|
2022-12-23 15:36:23 +00:00
|
|
|
|
|
|
|
# Copy package.json to be able to execute migration command
|
|
|
|
COPY --chown=node:node package*.json ./
|
|
|
|
|
|
|
|
# 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" ]
|