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

Secret — 굽지 마, commit 하지 마

~14 min · security, secrets

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

절대 하지 말아야 할 것 셋

  1. Dockerfile 에 ENV API_KEY=... — secret 을 image 의 layer 에 굽는 거. image 가진 사람이 키 가짐.
  2. git 에 commit 한 compose.yaml 의 하드코딩 environment: — secret 이 영원히 repo 히스토리에.
  3. 런타임 용도에 --build-arg 로 secret 전달 — ARG 값이 image build 히스토리에 보임.

해야 할 것 셋

  1. 로컬 dev: .env 파일 compose 가 참조, .env.gitignore 에.
  2. CI/CD: pipeline 이 secret 을 런타임 환경에 주입 (GitHub Actions secrets, GitLab CI variables), image 에 X.
  3. Production: 진짜 secrets manager (AWS Secrets Manager, GCP Secret Manager, HashiCorp Vault, k8s 의 sealed-secrets) — 시작 시 fetch, 디스크에 안 씀.

Code

.env 와 compose (dev)·yaml
# .env (in .gitignore, never committed)
# DB_PASSWORD=secret
# API_KEY=sk-real-key-here

# compose.yaml
services:
  api:
    image: myapi:1.0
    environment:
      DB_PASSWORD: ${DB_PASSWORD}
      API_KEY: ${API_KEY}

# Compose auto-loads .env from the same directory.
BuildKit secret — build-time only·dockerfile
# syntax=docker/dockerfile:1
FROM python:3.12-slim
WORKDIR /app

COPY requirements.txt .
# Mount a secret only during this RUN — not stored in image
RUN --mount=type=secret,id=pip_creds \
    pip install --no-cache-dir -r requirements.txt \
      --index-url https://$(cat /run/secrets/pip_creds)@private.example.com/pypi

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

# Build with:
#   echo 'user:token' | docker build --secret id=pip_creds,src=/dev/stdin -t myapp .

External links

Exercise

본인 프로젝트에서 credential 이 Dockerfile, compose.yaml, 또는 git commit 된 데에 들어간 거 찾아. dev 용으로 .env (gitignore) 에 옮기고, README 에 — production 이 어떻게 진짜 secret store 에서 fetch 해야 하는지 — 문서화. diff 보여.

Progress

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

댓글 0

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

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