perf(docker): optimize backoffice Dockerfile

- Add --prefer-offline for faster installs
- Reduce NODE_OPTIONS to 384MB
- Add pnpm store prune to cleanup unused packages
- Unique cache id (pnpm-backoffice) to avoid conflicts
- Cleanup caches in same layer
This commit is contained in:
Tiago Yamamoto 2025-12-28 11:21:57 -03:00
parent a9d6ac0305
commit 3d704191e1

View file

@ -1,10 +1,12 @@
# =============================================================================
# GoHorse Backoffice - Optimized Dockerfile with pnpm + BuildKit caching
# Target: < 200MB final image
# GoHorse Backoffice - Ultra-Optimized Dockerfile with pnpm
# Target: < 150MB final image, minimal disk usage during build
# =============================================================================
# syntax=docker/dockerfile:1
# Stage 1: Base with pnpm (latest stable)
# -----------------------------------------------------------------------------
# Stage 1: Base with pnpm
# -----------------------------------------------------------------------------
FROM mirror.gcr.io/library/node:20-alpine AS base
# Enable corepack and activate pnpm
@ -13,69 +15,72 @@ RUN corepack enable && corepack prepare pnpm@latest --activate
# libc6-compat for native module compatibility
RUN apk add --no-cache libc6-compat
# Set pnpm store location for caching
# Set pnpm store for caching
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
# =============================================================================
# Stage 2: Install dependencies with BuildKit cache
# =============================================================================
# -----------------------------------------------------------------------------
# Stage 2: Dependencies
# -----------------------------------------------------------------------------
FROM base AS deps
WORKDIR /app
# Copy only package files first (better layer caching)
# Copy package files
COPY package.json pnpm-lock.yaml ./
# Use BuildKit cache mount for pnpm store - dramatically speeds up rebuilds
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
pnpm install --frozen-lockfile
# Install with cache mount - reutiliza entre builds
RUN --mount=type=cache,id=pnpm-backoffice,target=/pnpm/store \
pnpm install --frozen-lockfile --prefer-offline
# =============================================================================
# Stage 3: Build application
# =============================================================================
# -----------------------------------------------------------------------------
# Stage 3: Builder (memory-optimized)
# -----------------------------------------------------------------------------
FROM base AS builder
# Reduce memory for build
ENV NODE_OPTIONS="--max-old-space-size=384"
WORKDIR /app
# Copy dependencies from deps stage
# Copy deps
COPY --from=deps /app/node_modules ./node_modules
# Copy source files
# Copy source (selective, not COPY . .)
COPY package.json pnpm-lock.yaml ./
COPY tsconfig*.json nest-cli.json ./
COPY src ./src
# Build the NestJS application
RUN pnpm build
# Build and cleanup in same layer
RUN pnpm build && \
rm -rf node_modules/.cache
# Prune dev dependencies for smaller production image
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
pnpm prune --prod
# Prune dev deps with cache
RUN --mount=type=cache,id=pnpm-backoffice,target=/pnpm/store \
pnpm prune --prod && \
pnpm store prune
# =============================================================================
# Stage 4: Production - Minimal runtime
# =============================================================================
FROM mirror.gcr.io/library/node:20-alpine AS production
# -----------------------------------------------------------------------------
# Stage 4: Production (minimal)
# -----------------------------------------------------------------------------
FROM mirror.gcr.io/library/node:20-alpine AS runner
# Security: create non-root user
# Security: non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nestjs -u 1001 -G nodejs
WORKDIR /app
# Copy only production artifacts (with proper ownership)
# Copy only production artifacts
COPY --from=builder --chown=nestjs:nodejs /app/dist ./dist
COPY --from=builder --chown=nestjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nestjs:nodejs /app/package.json ./
# Environment configuration
ENV NODE_ENV=production
ENV BACKOFFICE_PORT=3001
ENV BACKOFFICE_HOST=0.0.0.0
ENV NODE_ENV=production \
BACKOFFICE_PORT=3001 \
BACKOFFICE_HOST=0.0.0.0
# Switch to non-root user for security
USER nestjs
EXPOSE 3001
# Start application
CMD ["node", "dist/main.js"]