OPTIMIZE: Simplify Docker multi-stage build to reduce space usage

- 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 <noreply@anthropic.com>
This commit is contained in:
ZacharyZcR
2025-09-24 02:28:00 +08:00
parent 017b56af27
commit b655b2fe0c

View File

@@ -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 ./