Files
invyone/docker/deploy/frontend.Dockerfile
T
johngreen 5fdd1c67b1 perf(CI): frontend build runner stage 를 Next.js standalone output 활용으로 전환
배경:
- frontend build 의 가장 큰 시간 소비 = runner stage 의 COPY node_modules (12분 16초)
- 전체 21분 34초 중 57%
- next.config.mjs 의 output: "standalone" 가 prod 빌드에서 이미 활성 상태였으나, Dockerfile 의 runner stage 가 .next 통째 + node_modules 통째를 COPY 하느라 standalone 결과물 미활용

조치:
- runner stage 재작성:
  - .next 전체 → .next/standalone (server.js + 실제 사용 node_modules)
  - .next/static 별도 COPY (standalone 가 자동 포함 안 함)
  - public 별도 COPY (standalone 가 자동 포함 안 함)
  - node_modules 통째 COPY 제거 (standalone 가 알아서 포함)
  - package.json COPY 제거 (server.js 직접 실행)
  - CMD: npm start → node server.js

검증:
- frontend 에 dynamic require/import 0건 (정적 import 만) → standalone 의존성 추적 정확
- prisma 가 package.json 에 있으나 코드 import 0건 → 자연 제외, 추가 설정 불필요

예상 효과:
- 빌드 시간 21m 34s → 약 9분 (12분 단축, 57% 감소)
- 이미지 크기 약 1GB → 약 300MB (70% 감소)
- pull 시간 단축
- runtime memory footprint 감소

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 16:44:36 +09:00

70 lines
2.1 KiB
Docker

# Multi-stage build for Next.js
FROM dockerhub.wace.me/node:20.19-alpine.linux AS base
# Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies
COPY package.json package-lock.json* ./
RUN npm install
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Disable telemetry during the build
ENV NEXT_TELEMETRY_DISABLED 1
# 빌드 시 환경변수 설정 (ARG로 받아서 ENV로 설정)
ARG NEXT_PUBLIC_API_URL=https://solution.invyone.com/api
ARG SERVER_API_URL=http://backend-spring.invyone.svc.cluster.local:8081
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
ENV SERVER_API_URL=$SERVER_API_URL
# Cache bust — GIT_SHA 가 매 commit 마다 다르므로 이 라인부터 아래 layer 가
# 항상 invalidate 되어 npm run build 가 새 source 로 다시 실행됨.
# (npm install layer 는 위쪽이라 영향 없음 — 빌드 시간 손해 없음.)
ARG GIT_SHA=unknown
ENV GIT_SHA=$GIT_SHA
RUN echo "Build SHA: $GIT_SHA"
# Build the application
ENV DISABLE_ESLINT_PLUGIN=true
ENV NODE_OPTIONS=--max-old-space-size=4096
RUN npm run build
# Production image — Next.js standalone output 활용
# next.config.mjs 의 `output: "standalone"` 이 빌드 시 .next/standalone/ 에
# server.js + 실제로 사용되는 node_modules 만 자동 포함. node_modules 통째 COPY 불필요.
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# public 폴더 (standalone 가 자동 포함하지 않음)
COPY --from=builder /app/public ./public
# standalone 빌드 결과: server.js + minimal node_modules
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# static asset (standalone 가 자동 포함하지 않음)
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
# standalone 의 server.js 직접 실행 (npm start 대신)
CMD ["node", "server.js"]