From 3845756c36f1a336bebb18230c9ca26f36af26a0 Mon Sep 17 00:00:00 2001 From: Grant Date: Tue, 2 Apr 2024 23:55:30 -0600 Subject: [PATCH] optimize dockerfile to download dependencies before all --- Dockerfile | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index b8086d0..a174051 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,33 @@ FROM node:20-alpine AS base +FROM base as dev_dep +RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app +WORKDIR /home/node/app + +# --- dependencies --- +COPY --chown=node:node package*.json ./ +COPY --chown=node:node packages/admin/package*.json ./packages/admin/ +COPY --chown=node:node packages/client/package*.json ./packages/client/ +COPY --chown=node:node packages/lib/package*.json ./packages/lib/ +COPY --chown=node:node packages/server/package*.json ./packages/server/ + +USER node +RUN npm install --include=dev + +FROM base as dep +RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app +WORKDIR /home/node/app + +# --- dependencies --- +COPY --chown=node:node package*.json ./ +COPY --chown=node:node packages/admin/package*.json ./packages/admin/ +COPY --chown=node:node packages/client/package*.json ./packages/client/ +COPY --chown=node:node packages/lib/package*.json ./packages/lib/ +COPY --chown=node:node packages/server/package*.json ./packages/server/ + +USER node +RUN npm install --omit=dev + # # === BUILDER === # @@ -7,10 +35,10 @@ FROM node:20-alpine AS base FROM base as build RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app WORKDIR /home/node/app -COPY --chown=node:node . . -USER node -RUN npm install --include=dev +COPY --from=dev_dep /home/node/app/ ./ + +COPY --chown=node:node . . # --- build lib --- @@ -37,6 +65,7 @@ RUN npm -w packages/server run build FROM base as run WORKDIR /home/node/app +COPY --from=dep /home/node/app/ ./ COPY package*.json docker-start.sh ./ # --- prepare lib --- @@ -65,7 +94,6 @@ COPY --from=build /home/node/app/packages/server/dist ./packages/server/dist # --- finalize --- -RUN npm install --omit=dev RUN npx -w packages/server prisma generate # set runtime env variables