From fd0de89089da8d25db9de7d52021ceb268eabe1c Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Sat, 16 Sep 2023 11:39:26 +0200 Subject: [PATCH] refactor(snackager): minimize final docker image as much as possible --- snackager/Dockerfile | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/snackager/Dockerfile b/snackager/Dockerfile index d25c445c..b9ebbfa7 100644 --- a/snackager/Dockerfile +++ b/snackager/Dockerfile @@ -1,17 +1,19 @@ ARG node_version -# --- Scoper - limit used workspaces for Snackager +# --- Scoper - Remove unused workspaces for Snackager FROM node:${node_version}-alpine as scoper WORKDIR /app # Copy repository COPY . . # Prune repository and remove unused workspaces -RUN yarn global add turbo@1.10.x && turbo prune --docker --scope=snack-modules-packager +RUN apk --no-cache add jq && \ + yarn global add turbo@$(jq --raw-output '.devDependencies.turbo' package.json) && \ + turbo prune --docker --scope=snack-modules-packager -# --- Builder - Build Snackager -FROM node:${node_version}-alpine as builder +# --- Development - Prepare Snackager for development +FROM node:${node_version}-alpine as development WORKDIR /app # Copy "package.json"-only repository @@ -30,8 +32,8 @@ WORKDIR /app/snackager CMD ["yarn", "start"] -# --- Production - Run Snackager in production -FROM node:${node_version}-alpine +# --- Deploy - Prepare Snackager for production +FROM node:${node_version}-alpine as deploy WORKDIR /app # Install system dependencies @@ -42,10 +44,24 @@ ARG APP_VERSION ENV APP_VERSION ${APP_VERSION} # Copy built repository -COPY --from=builder /app . +COPY --from=development /app . +# Ensure Snackager is build, has dependencies, and remove unnecessary dependencies from workspaces +RUN \ + yarn build:snackager && \ + yarn install --frozen-lockfile --production + # find . -path 'packages/*/node_modules/*' -delete + + +# --- Production - Run Snackager in production +FROM node:${node_version}-alpine +WORKDIR /app + +# Prepare environment variables +ARG APP_VERSION +ENV APP_VERSION ${APP_VERSION} -# Ensure Snackager is built and only has production dependencies -RUN yarn build:snackager && yarn install --frozen-lockfile --production +# Copy deployment-ready repository +COPY --from=deploy /app . # Setup Snackager for production WORKDIR /app/snackager