From b655b2fe0c35c2ab8d0f59962cc4920fa3106abf Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Wed, 24 Sep 2025 02:28:00 +0800 Subject: [PATCH] OPTIMIZE: Simplify Docker multi-stage build to reduce space usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Merge production-deps and native-builder stages to eliminate duplication - Remove redundant intermediate layers that were consuming Docker space - Add aggressive cleanup (rm -rf ~/.npm /tmp/* /var/cache/apk/*) - Reduce overall image size and build-time space requirements Fixes: - ENOSPC errors during COPY operations from multiple build stages - Excessive Docker layer accumulation from duplicate dependency installs - Reduced disk space usage during multi-stage builds 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- docker/Dockerfile | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index c9fd17b7..0d8419df 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -35,35 +35,23 @@ RUN npm rebuild better-sqlite3 --force RUN npm run build:backend -# Stage 4: Production dependencies +# Stage 4: Production dependencies with native modules FROM node:24-alpine AS production-deps WORKDIR /app +RUN apk add --no-cache python3 make g++ + COPY package*.json ./ ENV npm_config_target_platform=linux ENV npm_config_target_arch=x64 ENV npm_config_target_libc=glibc +# Install production dependencies and rebuild native modules in one stage RUN npm ci --omit=dev --ignore-scripts --force && \ - npm cache clean --force - -# Stage 5: Build native modules -FROM node:24-alpine AS native-builder -WORKDIR /app - -RUN apk add --no-cache python3 make g++ - -COPY package*.json ./ - -ENV npm_config_target_platform=linux -ENV npm_config_target_arch=x64 -ENV npm_config_target_libc=glibc - -# Install native modules and compile them properly -RUN npm ci --omit=dev --force && \ npm rebuild better-sqlite3 bcryptjs --force && \ - npm cache clean --force + npm cache clean --force && \ + rm -rf ~/.npm /tmp/* /var/cache/apk/* # Stage 6: Final image FROM node:24-alpine @@ -83,7 +71,7 @@ RUN chown -R nginx:nginx /usr/share/nginx/html WORKDIR /app -COPY --from=native-builder /app/node_modules /app/node_modules +COPY --from=production-deps /app/node_modules /app/node_modules COPY --from=backend-builder /app/dist/backend ./dist/backend COPY package.json ./