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

Non-root + 리소스 한도 — 두 디폴트

~14 min · security, production

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

항상 non-root 로

디폴트로 container 안 process 는 root 로 돌아. 앱에 취약점 있고 attacker 가 namespace 탈출하면 — 호스트에서 root. 시스템 user 만들고 CMD 전에 전환하는 거 두 줄 비용, escalation 경로 제거.

리소스 한도 가 noisy neighbor 막음

--memory--cpus 가 kernel 한테 container 가 얼마 쓸 수 있는지 알려줌. 없으면 버그 있는 container 가 호스트 OOM 시켜서 다 같이 다운.

Code

Dockerfile non-root·dockerfile
FROM python:3.12-slim
WORKDIR /app

# Install everything as root
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .

# Create system user/group, then switch BEFORE CMD
RUN addgroup --system app && adduser --system --group app
USER app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
런타임 리소스 한도·bash
# Cap memory at 512MB, swap at 1GB total
docker run -d --memory=512m --memory-swap=1g myapp

# Cap CPU at 1.5 cores
docker run -d --cpus=1.5 myapp

# Watch live
docker stats --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}'

External links

Exercise

Root 로 도는 Dockerfile 가져와. 시스템 user 생성 + USER app 줄 추가. Build, run, docker exec ... whoami 로 process 가 더 이상 root 아닌 거 확인. 그 다음 --memory=128m 으로 돌리고 앱이 그거 넘으면 어떻게 되는지 봐.

Progress

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

댓글 0

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

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