C.W.K.
Stream
Lesson 03 of 08 · published

Docker

~22 min · Docker, standalone, multi-stage

Level 0Curious
0 XP0/68 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Standalone output

next.config.tsoutput: 'standalone' 설정하고 next build 가 runtime 에 필요한 file 만 trace + minimal server.js. 결과는 작은 container 에 ship 가능한 self-contained directory (보통 < 100MB).

Multi-stage Dockerfile

3 stage: deps 가 node_modules install, buildernext build 돌리고, runner 가 standalone output 만 copy. 각 stage 가 깨끗한 image 로 시작 — 최종 layer 가 도는 거만.

Static asset 잊지 마

Standalone 이 application code bundle; .next/staticpublic/ 는 runner stage 로 직접 copy.

Code

Standalone output 활성화·ts
// next.config.ts
import type { NextConfig } from 'next';
const config: NextConfig = {
  output: 'standalone',
};
export default config;
Multi-stage Dockerfile·text
FROM node:20-alpine AS base

FROM base AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME=0.0.0.0

COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public

EXPOSE 3000
CMD ["node", "server.js"]
Build 와 run·bash
docker build -t my-nextjs-app .
docker run -p 3000:3000 \
  -e DATABASE_URL=postgresql://host/db \
  -e AUTH_SECRET=mysecret \
  my-nextjs-app

External links

Exercise

Multi-stage Dockerfile 로 Next.js app containerize. output: 'standalone' 있/없 image size 비교. Local 에서 돌려서 env var 가 pick up 되는지 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.