Docker 파일 기반의 CI/CD 프로세스 최적화

By: 준영 김
Posted: September 06, 2023

docker 파일을 셋팅하고 최적화 작업을 한 내용을 기록용

최적화 구성

멀티스테이지 구성

FROM node:20.5.1-alpine3.17 AS base

FROM base AS deps

FROM base AS images

FROM base AS builder

FROM base AS runner

base 스테이지

deps 스테이지

COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./

images 스테이지

FROM base AS images
WORKDIR /app
COPY ./public/res ./public/res

builder 스테이지

An image from Notion
FROM node:20-alpine AS base
An image from Notion

This variant is useful when final image size being as small as possible is your primary concern. The main caveat to note is that it does use musl libc⁠ instead of glibc and friends⁠, so software will often run into issues depending on the depth of their libc requirements/assumptions. See this Hacker News comment thread⁠ for more discussion of the issues that might arise and some pro/con comparisons of using Alpine-based images.

COPY package.json pnpm-lock.yaml* ./
// package.json
// ...
"packageManager": "pnpm@8.0.0",
"pnpm": {
  "peerDependencyRules": {
    "allowedVersions": {
      "react": "^18",
      "react-dom": "^18"
    }
  },
 // ...
# ci.yaml
- name: Next.js cache
  uses: actions/cache@v3
  with:
	  path: ${{ github.workspace }}/.next/cache
		key: ${{ runner.os }}-${{ runner.node }}-${{ hashFiles('**/pnpm-lock.yaml') }}-nextjs
An image from Notion
# ci.yaml
- name: export Next.js cache
	uses: docker/build-push-action@v5
	with:
		context: .
		file: ./Dockerfile
		target: cache
		outputs: type=local,dest=.
# Dockerfile
COPY .next/cache ./.next/cache

runner 스테이지

기본 문법

디렉티브 작성

# syntax=docker/dockerfile:1.4

레퍼런스

ENV NEXT_TELEMETRY_DISABLED 1
const nextConfig = getConfig({
  // ...
  output: 'standalone',
  // ...
});
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

https://jaynote2022.tistory.com/entry/README-19

https://patrick-f.tistory.com/59

https://kyoung-jnn.com/posts/nextjs-build-optimization-in-ci

https://jadon.tistory.com/46