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

Layer 캐싱 — 순서가 중요해

~16 min · dockerfile, performance

Level 0Container 호기심
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

각 명령은 layer; 각 layer 는 캐시

Docker 는 각 명령을 캐시랑 비교. 명령이랑 입력 (COPY 가 참조하는 파일, RUN 이 쓰는 arg) 안 바뀌면 캐시 layer 재사용. 캐시 미스 나는 첫 명령이 그 이후 모든 layer 깨.

캐시 깨기 버그

RUN pip install 전에 COPY . . 하면 — 코드 한 줄 바꿔도 다 다시 복사 + install layer 깨짐 + 모든 의존성 재설치. 느림. 짜증. 쉽게 피할 수 있어.

해결: 의존성 먼저, 코드 나중

의존성 manifest (requirements.txt, package.json, Cargo.toml) 먼저 복사. install. 그 다음에 나머지 소스 복사. 코드 변경이 의존성 layer 안 깨.

Code

❌ BAD: 코드 바꿀 때마다 의존성 재설치·dockerfile
FROM python:3.12-slim
WORKDIR /app

COPY . .                              # ← code AND requirements
RUN pip install -r requirements.txt    # ← reruns on every code edit

CMD ["python", "main.py"]
✅ GOOD: 의존성 따로 캐시·dockerfile
FROM python:3.12-slim
WORKDIR /app

COPY requirements.txt .                # ← only the manifest
RUN pip install --no-cache-dir -r requirements.txt

COPY . .                              # ← code copied AFTER deps install

CMD ["python", "main.py"]

# Now: editing main.py rebuilds only the COPY . . layer.
# pip install layer stays cached until requirements.txt changes.
Node 도 같은 아이디어·dockerfile
FROM node:22-alpine
WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build

CMD ["node", "dist/server.js"]

External links

Exercise

본인 프로젝트의 Dockerfile 가져와. --no-cache 로 cold build 시간 재. 코드 한 줄 바꾸고 docker build 다시. 시간 재. 그 다음 의존성 manifest copy 를 소스 copy 위로 옮겨. 두 시간 다시 재. 네 숫자 보고.

Progress

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

댓글 0

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

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