Skip to content

Commit f1b3455

Browse files
committed
docker
1 parent a3897e0 commit f1b3455

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

‎Dockerfile‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Multi-stage build
2+
3+
# 1단계: 환경 설정 및 dependancy 설치
4+
FROM node:20-alpine AS deps
5+
RUN apk add --no-cache libc6-compat
6+
7+
# 명령어를 실행할 디렉터리 지정
8+
WORKDIR /app
9+
10+
# Dependancy install을 위해 package.json, package-lock.json, yarn.lock 복사
11+
COPY package.json yarn.lock ./
12+
13+
# Dependancy 설치 (새로운 lock 파일 수정 또는 생성 방지)
14+
RUN yarn --frozen-lockfile
15+
16+
###########################################################
17+
#
18+
# 2단계: next.js 빌드 단계
19+
FROM node:20-alpine AS builder
20+
21+
# 명령어를 실행할 디렉터리 지정
22+
WORKDIR /app
23+
24+
# node_modules 등의 dependancy를 복사함.
25+
COPY --from=deps /app/node_modules ./node_modules
26+
COPY . .
27+
28+
RUN yarn build
29+
30+
###########################################################
31+
32+
# 3단계: next.js 실행 단계
33+
FROM node:20-alpine AS runner
34+
35+
# 명령어를 실행할 디렉터리 지정
36+
WORKDIR /app
37+
38+
# container 환경에 시스템 사용자를 추가함
39+
RUN addgroup --system --gid 1001 nodejs
40+
RUN adduser --system --uid 1001 nextjs
41+
42+
# next.config.js에서 output을 standalone으로 설정하면
43+
# 빌드에 필요한 최소한의 파일만 ./next/standalone로 출력이 된다.
44+
# standalone 결과물에는 public 폴더와 static 폴더 내용은 포함되지 않으므로, 따로 복사를 해준다.
45+
COPY --from=builder /app/public ./public
46+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
47+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
48+
49+
# 컨테이너의 수신 대기 포트를 3000으로 설정
50+
EXPOSE 3000
51+
52+
# node로 애플리케이션 실행
53+
CMD ["node", "server.js"]
54+
55+
# standalone으로 나온 결과값은 node 자체적으로만 실행 가능
56+
# CMD ["npm", "start"]

0 commit comments

Comments
 (0)