From 52b1b04d9b13051c7544e4eeb53a7a2b454af5ad Mon Sep 17 00:00:00 2001 From: Bill Chirico Date: Mon, 6 Nov 2023 11:44:32 -0500 Subject: [PATCH] feat(Dockerfile): Add multi-stage build for production deployment - Added a new stage "deps" to install production dependencies only - Added a new stage "builder" to build the application - Copied necessary files from the "builder" stage to the final image - Exposed port 5000 for the application - Updated CMD command to run the application This commit introduces a multi-stage build process in the Dockerfile, which separates dependency installation and application building. This improves efficiency and reduces the size of the final Docker image. --- Dockerfile | 44 +++++++++++--------------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6e8cf18..e9de40c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,43 +1,20 @@ -# FROM node:lts-alpine - -# LABEL name="Docusaurus on docker latest-stable" \ -# maintainer="VolvoxLLC " \ -# version="1.0.0" \ -# release="latest" \ -# url="https://github.com/VolvoxLLC/Volvox.Apollo.Docs" \ -# summary="Volvox.Apollo Docs Docker \ -# on Node 20.x, lts-alpine" \ -# description="Volvox.Apollo Docs \ -# on Node 20.x, lts-alpine" - -# # run as our node user from base image -# # we delete the dockerfiles we don't need -# # this leaves us with a default v1 docusarus install -# # we can mount our own into the container -# USER node -# RUN mkdir ~/npm-global \ -# && npm config set prefix '~/npm-global' \ -# && echo 'export PATH=~/npm-global/bin:$PATH' > ~/.profile \ -# && mkdir -p /home/node/docs \ -# && cd /home/node/docs \ -# && npm install --global docusaurus-init \ -# && sh -l -c docusaurus-init - -# EXPOSE 5001/tcp -# USER node -# WORKDIR /home/node/docs - -# CMD ["sh", "-l", "-c", "npm run serve"] - -## Base ######################################################################## # Use a larger node image to do the build for native deps (e.g., gcc, python) FROM node:lts-alpine AS deps +WORKDIR /opt/app +# Copy package.json and package-lock.json to the working directory +COPY package.json package-lock.json ./ +# Install production dependencies only +RUN npm ci --only=production + # Stage 2: Build the application FROM node:lts-alpine AS builder WORKDIR /opt/app # Copy all files from the current directory to the working directory in the container +# Copy node_modules from the "deps" stage +COPY package.json package-lock.json ./ +COPY --from=deps /opt/app/node_modules ./node_modules COPY . . # Build the application RUN npm run build @@ -50,5 +27,6 @@ WORKDIR /opt/app ENV NODE_ENV=production # Copy necessary files from the "builder" stage EXPOSE 5000 +COPY --from=builder /opt/app/build ./build # Define the command to run the application -CMD ["sh", "-l", "-c", "npm run serve"] +CMD ["sh", "-l", "-c", "npm run serve"] \ No newline at end of file