Sub topic 3
Ordering layers for fast rebuilds
This chapter assumes you have a requirements.lock file from the earlier chapters, Docker installed, and a working knowledge of Git. Each Dockerfile instruction creates a layer, and Docker reuses a cached layer when its instruction and inputs have not changed. Put slow, rarely changing steps first.
Ordering layers for fast rebuilds
Copy the lock file and install from it before copying source code. Source code changes on almost every edit, but the lock file changes only when dependencies change, so the dependency layer stays cached.
# Replace the digest with the one from the step below
FROM python:3.11-slim@sha256:<digest>
WORKDIR /app
COPY requirements.lock .
RUN pip install --no-cache-dir --require-hashes -r requirements.lock
COPY src/ ./src/
CMD ["python", "-m", "src.train"]Pinning the base image by digest
A tag such as python:3.11-slim can point to a new image when the publisher rebuilds it. A digest names one exact image. Get it after pulling:
docker pull python:3.11-slim
docker inspect --format '{{index .RepoDigests 0}}' python:3.11-slimRunning GPU workloads in containers
The image carries the CUDA user-space libraries, while the host supplies the NVIDIA driver. The driver must support the CUDA version in the image. The NVIDIA Container Toolkit exposes the GPUs to the container:
docker build -t ml-train:dev .
docker run --rm --gpus all ml-train:devOne workflow from lock file to running container
Regenerate the lock file, build the image, and run it. Commit the lock file and Dockerfile together.
pip-compile --generate-hashes -o requirements.lock requirements.in
docker build -t ml-train:dev .
docker run --rm --gpus all ml-train:devSeeds and nondeterministic operations
Seeds fix the Python, NumPy, and PyTorch generators, and deterministic mode makes PyTorch raise an error on nondeterministic operations:
import random
import numpy as np
import torch
random.seed(42)
np.random.seed(42)
torch.manual_seed(42)
torch.use_deterministic_algorithms(True)Some GPU kernels, data loader worker ordering, and hardware differences remain as sources of variation.
Checking reproducibility in CI
Fail the build when the lock file is stale or the image cannot be built:
pip-compile --generate-hashes -o /tmp/check.lock requirements.in
diff -u requirements.lock /tmp/check.lock
docker build -t ml-train:ci .Updating locked dependencies safely
Upgrade one package at a time, run the tests, rebuild the image, and commit the new lock file with the change:
pip-compile --upgrade-package numpy --generate-hashes -o requirements.lock requirements.inVersioning data and models next to the environment
Record the dataset version and the model artifact hash alongside the image tag, in the same commit. Then any result can be traced to its code, environment, and data.
Exercise
Why does copying src/ after the dependency install make rebuilds faster?
Show answer
Source edits change only the later layer. Docker reuses the cached dependency layer, so the slow pip install does not run again.