Reduce docker image size

This commit is contained in:
Ahmed DCHAR 2026-04-12 00:16:27 +02:00
parent f932664055
commit 0216a9b521
2 changed files with 32 additions and 13 deletions

View File

@ -3,19 +3,20 @@ FROM node:22-alpine AS builder
WORKDIR /app WORKDIR /app
# Copy package files # Copy package files first (better cache layer)
COPY package.json package-lock.json ./ COPY package.json package-lock.json ./
# Install dependencies (including dev for build) # Install dependencies (include optional for native modules like oxc-parser)
RUN npm install --legacy-peer-deps RUN npm install --legacy-peer-deps && \
npm cache clean --force
# Copy app source # Copy app source
COPY . . COPY . .
# Build app # Build app with higher memory limit (prerendering is memory-intensive)
RUN npm run build RUN NODE_OPTIONS="--max-old-space-size=2048" npm run build
# Runtime stage # Runtime stage - use distroless for minimal size
FROM node:22-alpine FROM node:22-alpine
WORKDIR /app WORKDIR /app
@ -23,18 +24,26 @@ WORKDIR /app
# Copy package files # Copy package files
COPY package.json package-lock.json ./ COPY package.json package-lock.json ./
# Install only production dependencies # Install production dependencies only with optimizations
RUN npm install --omit=dev --legacy-peer-deps RUN npm install --omit=dev --legacy-peer-deps && \
npm cache clean --force && \
# Remove unnecessary files
rm -rf /root/.npm /root/.cache
# Copy built app from builder # Copy built app from builder
COPY --from=builder /app/.output ./.output COPY --from=builder /app/.output ./.output
# Expose port (adjust if needed) # Use non-root user for security
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001
USER nodejs
EXPOSE 3000 EXPOSE 3000
# Health check # Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \ HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/', (r) => {if (r.statusCode !== 200) throw new Error(r.statusCode)})" CMD node -e "require('http').get('http://localhost:3000/', (r) => {if (r.statusCode !== 200) throw new Error(r.statusCode)})"
# Start app # Start app with memory limits
CMD ["node", ".output/server/index.mjs"] CMD ["node", "--max-old-space-size=512", ".output/server/index.mjs"]

View File

@ -7,7 +7,17 @@ services:
- "3000:3000" - "3000:3000"
environment: environment:
- NODE_ENV=production - NODE_ENV=production
- NODE_OPTIONS=--max-old-space-size=512
volumes: volumes:
- ../botasaurus/content/novels:/app/content/novels - ../botasaurus/content/novels:/app/content/novels:ro
- ../botasaurus/public/images:/app/public/images - ../botasaurus/public/images:/app/public/images:ro
restart: unless-stopped restart: unless-stopped
# Resource limits for 2vCPU VPS
deploy:
resources:
limits:
cpus: '1.5'
memory: 1G
reservations:
cpus: '1'
memory: 512M