[docker build] 도커 설정 파일 추가

This commit is contained in:
2025-06-26 17:59:40 +09:00
parent 5aadc38441
commit c05300503a
3 changed files with 167 additions and 0 deletions
+93
View File
@@ -0,0 +1,93 @@
# ---------------------- #
# STEP 1: Base 설치 환경 #
# ---------------------- #
FROM node:20-alpine AS base
WORKDIR /repo
# pnpm 설치 (corepack)
RUN corepack enable && corepack prepare pnpm@10.12.1 --activate
# 필요한 시스템 패키지 설치 (선택)
RUN apk add --no-cache libc6-compat
# ---------------------- #
# STEP 2: 의존성 설치용 캐시 레이어 #
# ---------------------- #
FROM base AS deps
# workspace 정의 복사
COPY pnpm-workspace.yaml ./
COPY package.json pnpm-lock.yaml ./
COPY apps/cs/package.json ./apps/cs/
COPY packages ./packages
# 종속성 설치 (only production dependencies)
RUN rm -rf node_modules .next
RUN pnpm install --frozen-lockfile --node-linker=node-modules
#RUN pnpm install --frozen-lockfile --node-linker=node-modules
RUN ls -al .
#RUN pnpm install --prod
# ---------------------- #
# STEP 3: 빌드 단계 #
# ---------------------- #
FROM base AS builder
COPY --from=deps /repo /repo
WORKDIR /repo
# 전체 소스 복사
COPY . .
RUN ls -al /repo/apps/cs/.next || true
RUN rm -rf /repo/apps/cs/.next
RUN ls -al /repo/apps/cs/
# Next.js 빌드
RUN cat /repo/apps/cs/next.config.ts || true
RUN pnpm --filter ./apps/cs... build
#RUN ls -al .next/standalone
# 👉 실행에 필요한 모듈만 유지
#RUN pnpm prune --prod
# Next.js 빌드 내용 보기
RUN ls -al ./apps/cs/
RUN ls -al /repo/apps/cs/.next
RUN ls -al /repo/apps/cs/.next/chunks || true
# web browser request zip format
RUN find /repo/apps/cs/.next/static -type f -name '*.js' -exec gzip -k {} \;
#RUN ls -al ./apps/cs/node-modules | wc -l
#RUN find ./apps/cs/.next -type f
# ---------------------- #
# STEP 4: 최종 러너 단계 (최소 런타임) #
# ---------------------- #
FROM node:20-alpine AS runner
WORKDIR /app
# corepack 사용 설정
#RUN corepack enable && corepack prepare pnpm@8.15.5 --activate
# 환경변수 설정
ENV NODE_ENV=production
ENV PORT=3000
# pnpm은 런타임에 필요 없음 → corepack 설치 생략 가능
# 빌드 결과만 복사
RUN rm -rf .next
COPY --from=builder /repo/apps/cs/.next/standalone ./
COPY --from=builder /repo/apps/cs/public ./public
COPY --from=builder /repo/apps/cs/.next/static ./apps/cs/.next/static
COPY --from=builder /repo/apps/cs/.env ./apps/cs/.env
COPY --from=builder /repo/apps/cs/package.json ./apps/cs/package.json
COPY --from=builder /repo/apps/cs/next.config.ts ./apps/cs/next.config.ts
# 헬스체크 등 필요 시 추가 가능
#HEALTHCHECK --interval=30s --timeout=3s CMD wget --spider -q http://localhost:3000 || exit 1
EXPOSE 3000
CMD ["node", "apps/cs/server.js"]
#CMD ["pnpm", "start"]
+36
View File
@@ -0,0 +1,36 @@
# Docker 이미지 빌드
docker build -t frontend .
# 로그 포함 빌드
docker build -t frontend --progress=plain .
docker scout quickview
# 실행
docker run -p 3000:3000 frontend
docker run --name frontend -p 3000:3000 frontend
#
docker run -it frontend sh
ls -al /app
# 실제 실행 방법
node .next/standalone/apps/cs/server.js
/app/.next # cp -r static /app/apps/cs/.next/static
• .env.production 파일을 .next/standalone/ 기준 또는 루트로 복사
• 또는 process.env.xxx를 직접 주입 (예: docker run -e)
일부 라이브러리는 process.cwd()를 기준으로 파일을 찾는데, 이 경로는 .next/standalone/... 안이므로 실패할 수 있습니다.
해결:
가능하면 __dirname 기반 경로를 사용하거나, 실행 전에 process.chdir(...) 수동 지정
4. ✅ 의존성 누락
Next.js가 standalone에 필요한 모듈만 복사하지만, 일부는 누락될 수 있습니다.
예:
• 동적 import (import('some-lib'))
• optional chaining으로 쓰이는 모듈
+38
View File
@@ -0,0 +1,38 @@
import type { NextConfig } from "next";
const subDomain =
process.env.NEXT_PUBLIC_SUB_DOMAIN || "http://localhost:13000";
const nextConfig: NextConfig = {
output: 'standalone', // docker image shrink
compress: true, // 기본값 true지만 명시 web browser request zip format
async rewrites() {
return [
{
source: '/_next/static/:path*.js',
destination: '/_next/static/:path*.js.gz',
has: [
{
type: 'header',
key: 'accept-encoding',
value: '.*gzip.*',
},
],
},
]
},
async headers() {
return [
{
source: '/_next/static/:path*.js.gz',
headers: [
{ key: 'Content-Encoding', value: 'gzip' },
{ key: 'Content-Type', value: 'application/javascript' },
{ key: 'Vary', value: 'Accept-Encoding' },
],
},
]
},
};
export default nextConfig;