From 2b325f346125fc8fe7f193d517a57aed1ef965ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gir=C3=B3n?= Date: Tue, 19 Aug 2025 11:17:32 +0200 Subject: [PATCH] feat: optimize Dockerfile (#47) * define base image arg * create base stage with common content * chmod executable entrypoint file this avoids re-copying the same file as is being modified in the docker layer * cache npm downloaded packages avoids re-downloading deps if cache content is available --- docker/Dockerfile | 29 +++++++++++++---------------- docker/docker-entrypoint.sh | 0 2 files changed, 13 insertions(+), 16 deletions(-) mode change 100644 => 100755 docker/docker-entrypoint.sh diff --git a/docker/Dockerfile b/docker/Dockerfile index 4d357cb..da577cc 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,21 +1,29 @@ # Dockerfile for Open Archiver -# 1. Build Stage: Install all dependencies and build the project -FROM node:22-alpine AS build +ARG BASE_IMAGE=node:22-alpine + +# 0. Base Stage: Define all common dependencies and setup +FROM ${BASE_IMAGE} AS base WORKDIR /app # Install pnpm -RUN npm install -g pnpm +RUN --mount=type=cache,target=/root/.npm \ + npm install -g pnpm # Copy manifests and lockfile COPY package.json pnpm-workspace.yaml pnpm-lock.yaml* ./ COPY packages/backend/package.json ./packages/backend/ COPY packages/frontend/package.json ./packages/frontend/ COPY packages/types/package.json ./packages/types/ + +# 1. Build Stage: Install all dependencies and build the project +FROM base AS build COPY packages/frontend/svelte.config.js ./packages/frontend/ # Install all dependencies. Use --shamefully-hoist to create a flat node_modules structure -RUN pnpm install --shamefully-hoist --frozen-lockfile --prod=false +ENV PNPM_HOME="/pnpm" +RUN --mount=type=cache,id=pnpm,target=/pnpm/store \ + pnpm install --shamefully-hoist --frozen-lockfile --prod=false # Copy the rest of the source code COPY . . @@ -24,17 +32,7 @@ COPY . . RUN pnpm build # 2. Production Stage: Install only production dependencies and copy built artifacts -FROM node:22-alpine AS production -WORKDIR /app - -# Install pnpm -RUN npm install -g pnpm - -# Copy manifests and lockfile -COPY package.json pnpm-workspace.yaml pnpm-lock.yaml* ./ -COPY packages/backend/package.json ./packages/backend/ -COPY packages/frontend/package.json ./packages/frontend/ -COPY packages/types/package.json ./packages/types/ +FROM base AS production # Install production dependencies # RUN pnpm install --shamefully-hoist --frozen-lockfile --prod=true @@ -48,7 +46,6 @@ COPY --from=build /app/packages/backend/src/database/migrations ./packages/backe # Copy the entrypoint script and make it executable COPY docker/docker-entrypoint.sh /usr/local/bin/ -RUN chmod +x /usr/local/bin/docker-entrypoint.sh # Expose the port the app runs on EXPOSE 4000 diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh old mode 100644 new mode 100755