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:
- Base image (e.g.,
nvidia/cuda:12.4-cudnn-runtime-ubuntu22.04). - System packages (apt-get).
- Python deps (requirements.txt / pyproject.toml).
- 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.