C.W.K.
Stream
Lesson 08 of 12 · published

Docker for ML

~11 min · docker, ml, cuda

Level 0Apprentice
0 XP0/101 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Reproducibility for ML environments

Python ML environments are notoriously fragile — CUDA version, driver version, PyTorch wheel build, cuDNN version, all interlocked. Docker freezes the whole stack into one image. CI builds and tests against that image instead of trying to recreate it on every run.

Layer for cache hits

Order Dockerfile layers by stability — most stable at top, most volatile at bottom:

  1. Base image (e.g., nvidia/cuda:12.4-cudnn-runtime-ubuntu22.04).
  2. System packages (apt-get).
  3. Python deps (requirements.txt / pyproject.toml).
  4. Source code (last — changes most often).

The first three layers cache for weeks; the last rebuilds on every commit. CI build time drops from 10 minutes to 30 seconds for typical PRs.

Multi-stage for slim runtime

Build stage installs build tools + compiles deps; runtime stage copies only the compiled deps + source. Final image often shrinks 5–10×.

CI cadence

  • Daily rebuild of the base 'ML deps' image — keeps the cache fresh.
  • Per-PR builds reuse the daily image; only the source layer changes.

Code

Multi-stage Dockerfile for an ML service·dockerfile
# syntax=docker/dockerfile:1
FROM nvidia/cuda:12.4-cudnn-runtime-ubuntu22.04 AS base
RUN apt-get update && apt-get install -y --no-install-recommends \
      python3.12 python3-pip git \
    && rm -rf /var/lib/apt/lists/*
ENV PIP_NO_CACHE_DIR=1

FROM base AS builder
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN pip install uv && uv sync --frozen --no-install-project

FROM base AS runtime
WORKDIR /app
COPY --from=builder /app/.venv /app/.venv
ENV PATH=/app/.venv/bin:$PATH
COPY src/ ./src/
CMD ["python", "-m", "src.app"]

External links

Exercise

If you have an ML service Dockerfile, audit layer order. Stable things on top, volatile on bottom. Time the build before and after — the diff is your cache discipline ROI.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.