# Base imageFROMnode:18# Create app directoryWORKDIR/usr/src/app# A wildcard is used to ensure both package.json AND package-lock.json are copiedCOPYpackage*.json ./
# Install app dependenciesRUNnpm install
# Bundle app sourceCOPY. .
# Creates a "dist" folder with the production buildRUNnpm run build
# Start the server using the production buildCMD["node","dist/main.js"]
#################### BUILD FOR LOCAL DEVELOPMENT###################FROMnode:18-alpineAsdevelopment# ... your development build instructions here#################### BUILD FOR PRODUCTION#################### Base image for productionFROMnode:18-alpineAsbuild# ... your build instructions here#################### PRODUCTION#################### Base image for productionFROMnode:18-alpineAsproduction# ... your production instructions here
#################### BUILD FOR LOCAL DEVELOPMENT###################FROMnode:18-alpineAsdevelopment# Create app directoryWORKDIR/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`RUNnpm ci
# Bundle app sourceCOPY--chown=node:node . .
# Use the node user from the image (instead of the root user)USERnode#################### BUILD FOR PRODUCTION###################FROMnode:18-alpineAsbuildWORKDIR/usr/src/appCOPY--chown=node:node package*.json ./
# In order to run `npm run build` we need access to the Nest CLI which 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 imageCOPY--chown=node:node --from=development /usr/src/app/node_modules ./node_modules
COPY--chown=node:node . .
# Run the build command which creates the production bundleRUNnpm run build
# Set NODE_ENV environment variableENVNODE_ENV production
# Running `npm ci` removes the existing node_modules directory and passing in --only=production ensures that only the production dependencies are installed. This ensures that the node_modules directory is as optimized as possibleRUNnpm ci --only=production && npm cache clean --force
USERnode#################### PRODUCTION###################FROMnode:18-alpineAsproduction# Copy the bundled code from the build stage to the production imageCOPY--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 buildCMD["node","dist/main.js"]