- Migrate from npm to pnpm for 2-3x faster installs - Add BuildKit cache mount for pnpm store - Auto-import package-lock.json to pnpm-lock.yaml - Reduce NODE_OPTIONS memory to 512MB - Add more exclusions to .dockerignore (cypress, e2e, tests) - Cleanup caches in same layer to save disk
87 lines
2.9 KiB
Docker
87 lines
2.9 KiB
Docker
# =============================================================================
|
|
# GoHorse Jobs Frontend - Ultra-Optimized Dockerfile with pnpm
|
|
# Target: < 150MB final image, minimal disk usage during build
|
|
# =============================================================================
|
|
# syntax=docker/dockerfile:1
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Stage 1: Base with pnpm
|
|
# -----------------------------------------------------------------------------
|
|
FROM mirror.gcr.io/library/node:20-alpine AS base
|
|
|
|
# Enable corepack and activate pnpm (latest)
|
|
RUN corepack enable && corepack prepare pnpm@latest --activate
|
|
|
|
# Set pnpm store for caching
|
|
ENV PNPM_HOME="/pnpm"
|
|
ENV PATH="$PNPM_HOME:$PATH"
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Stage 2: Dependencies
|
|
# -----------------------------------------------------------------------------
|
|
FROM base AS deps
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy package files (pnpm will use package-lock.json if no pnpm-lock.yaml)
|
|
COPY package.json package-lock.json* pnpm-lock.yaml* ./
|
|
|
|
# Install deps with cache mount - dramatically reduces disk usage on rebuilds
|
|
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
|
|
pnpm import 2>/dev/null || true && \
|
|
pnpm install --frozen-lockfile || pnpm install
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Stage 3: Builder (memory-optimized)
|
|
# -----------------------------------------------------------------------------
|
|
FROM base AS builder
|
|
|
|
# Reduce memory usage during build
|
|
ENV NODE_OPTIONS="--max-old-space-size=512"
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy deps from previous stage
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
|
|
# Copy source files (respects .dockerignore)
|
|
COPY package.json next.config.* tsconfig.json ./
|
|
COPY public ./public
|
|
COPY src ./src
|
|
|
|
# Build arguments
|
|
ARG NEXT_PUBLIC_API_URL=http://localhost:8080
|
|
ARG NEXT_PUBLIC_BACKOFFICE_URL=http://localhost:3001
|
|
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
|
|
ENV NEXT_PUBLIC_BACKOFFICE_URL=$NEXT_PUBLIC_BACKOFFICE_URL
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
|
|
# Build and cleanup in same layer
|
|
RUN pnpm build && \
|
|
rm -rf node_modules/.cache .next/cache
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Stage 4: Production Runner (minimal)
|
|
# -----------------------------------------------------------------------------
|
|
FROM mirror.gcr.io/library/node:20-alpine AS runner
|
|
|
|
WORKDIR /app
|
|
|
|
# Security: non-root user
|
|
RUN addgroup -g 1001 -S nodejs && \
|
|
adduser -u 1001 -S nextjs -G nodejs
|
|
|
|
ENV NODE_ENV=production \
|
|
NEXT_TELEMETRY_DISABLED=1 \
|
|
PORT=3000
|
|
|
|
# Copy ONLY standalone output (smallest possible)
|
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
|
|
USER nextjs
|
|
|
|
EXPOSE 3000
|
|
|
|
CMD ["node", "server.js"]
|