Skip to content

Instantly share code, notes, and snippets.

@mr-pascal
Created July 3, 2022 08:44
Show Gist options
  • Save mr-pascal/288f16fab400c2de6c0e0d57bdfe1385 to your computer and use it in GitHub Desktop.
Save mr-pascal/288f16fab400c2de6c0e0d57bdfe1385 to your computer and use it in GitHub Desktop.
ARG BUILD_IMAGE="node:18-slim"
ARG RUN_IMAGE="node:18-alpine"
##############################
######## build stage #########
FROM $BUILD_IMAGE AS builder
## NEW!!
ARG SRC_DIR=""
WORKDIR /app
## Copy package.json files for caching dependencies
## NEW!!
COPY ./${SRC_DIR}/package.json ./
## NEW!!
COPY ./${SRC_DIR}/package-lock.json ./
## Do a clean install of NPM packages
RUN npm ci
## Copy the rest of the files, including the actual sources
## NEW!!
COPY ${SRC_DIR} .
## Build the application.
RUN npm run build
################################
######## runtime stage #########
FROM $RUN_IMAGE as runtime
WORKDIR /app
## Copy package.json files
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/package-lock.json ./package-lock.json
## Install only the production dependencies
RUN npm ci --only=production
## Copy the pre-built application from the 'build' stage to 'runtime' stage
COPY --from=builder /app/dist ./dist
EXPOSE 3000
## Start application in production mode
CMD ["npm", "run", "start:prod"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment