Week 18 • Day 5Week 18

Day 5:Docker Multi-Stage Builds & Non-Root Security for AI Services

Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz

Day 5: Docker Multi-Stage Builds & Non-Root Security for AI Services

1. First-Principles Problem: The 4GB Bloated Container Anti-Pattern

Beginner AI Dockerfiles often begin with FROM python:3.12 and run pip install torch transformers fastapi:

  • Image Size: 4.2 GB
  • Container Pull / Cold Start: 90+ seconds in Kubernetes autoscaling.
  • Security Vulnerabilities: Contains full C compilers (gcc), package managers, and root permissions. If an attacker achieves code execution inside the container, they inherit root (UID 0) and can attempt kernel privilege escalation.

2. Multi-Stage Build Pattern with uv

A Multi-Stage Build physically separates the build environment (compilers, build headers, pip caches) from the runtime production artifact:

Architecture Flow Diagram
Install uv->B2[uv sync --frozen
Venv[Compiled /app/.venv
-.->|COPY --from=builder| R1[Clean Virtualenv
COPY src/ ./src
USER 10001 (Non-Root)

Production Results:

  • Image size shrinks from **$3.5\text{ GB}toto< 180\text{ MB}** (\95%$ reduction!).
  • Kubernetes cold start latency drops from $90\text{s}toto< 3\text{s}$.
  • Zero compiler binaries left in the production image.

3. Defense-in-Depth Container Hardening

  1. Explicit Non-Root User (USER 10001): The application runs as an unprivileged UID without write access to OS binaries (/bin, /etc).
  2. Read-Only Root Filesystem: Prevents malware from writing persistence scripts.
  3. Strict .dockerignore: Guarantees secrets (.env), test artifacts, and git histories are never baked into image layers.

In Simple English: Shrinking AI Docker Containers From 4 GB to 220 MB (Multi-Stage & Non-Root)

  • Rookie Mistake (4.2 GB image): Leaves C compilers, pip build caches, .git history, and test data inside the production Docker container — slow deploys AND huge hacker attack surface!
  • Multi-Stage Build (uv): Stage 1 (builder) installs compilers & downloads dependencies into /app/.venv. Stage 2 (runtime) copies ONLY /app/.venv + clean src/ onto a tiny slim image (~150–250 MB!).
  • Non-Root Security (USER appuser): Even if an attacker exploits a Python bug, they land as a locked low-privilege user instead of Linux root!