Sub topic 14
One workflow from lock file to running container
This chapter ties the earlier pieces into one repeatable path. A lock file pins the Python packages. A digest-pinned base image pins the operating system layer and Python. A single command sequence turns both into a running training job. It assumes you can read a Dockerfile, have Docker installed, and know the difference between requirements.in (declared) and a lock file (resolved).
The path in one picture
Each step produces an artifact that the next step consumes. Nothing is resolved or downloaded "fresh" after the lock file exists.
The lock file and the base image are the two inputs you control and commit. The host GPU driver is the third input. It is not in either artifact, and the image cannot pin it.
What each layer pins
| Layer | Pinned by | Example |
|---|---|---|
| Operating system, system libraries, Python | Base image digest | `python:3.11-slim@sha256:<digest>` |
| Python packages and their hashes | Lock file | `requirements.lock` |
| Your code | Git commit | `src/` |
| GPU driver | Not pinned. It comes from the host. | NVIDIA driver on the machine |
If a result changes, work down this table. Find out which layer changed, then check that layer's pin.
Step 1: Resolve the lock file where it will be used
A lock file records which wheels (prebuilt packages) were chosen for one platform and Python version. If you compile it on a macOS laptop and install it in a Linux container, the pinned versions and hashes may not exist for Linux. The safest habit is to compile inside the same base image the Dockerfile uses.
docker run --rm -v "$PWD":/work -w /work python:3.11-slim \
sh -c "pip install --quiet pip-tools && pip-compile --generate-hashes --quiet -o requirements.lock requirements.in"The container writes requirements.lock into your working directory. On Linux the file may be owned by root, so fix ownership with chown if your editor complains. Commit both requirements.in and requirements.lock. Review the lock file diff like code, because a changed version there is a changed experiment.
If your project uses uv instead, uv lock writes uv.lock. uv sync --locked then installs from it and fails when the lock file is out of date with pyproject.toml. Everything below applies the same way. Only the install line in the Dockerfile changes.
Step 2: Build the image from the lock file only
The Dockerfile from the previous chapter does the work. Two properties matter here. It installs with --require-hashes, so a package that is missing from the lock file or has a changed hash stops the build. It also copies the lock file before the source, so a code edit does not reinstall dependencies.
# In a real project, pin the base image by digest: python:3.11-slim@sha256:<digest>
FROM python:3.11-slim
WORKDIR /app
# Copy only the lock file first so this layer is reused until dependencies change
COPY requirements.lock .
RUN pip install --no-cache-dir --require-hashes -r requirements.lock
# Application code changes often, so copy it last
COPY src/ ./src/
CMD ["python", "-m", "src.train"]The FROM line must use the same image as the resolve step in step 1. A tag such as 3.11-slim is a moving label. The maintainers can point it at a new build, with a new patch release of Python or new system libraries, without telling you. A digest names exactly one image. To get one, pull the image and read it from docker image inspect --format '{{index .RepoDigests 0}}' python:3.11-slim. Then use that value in both the FROM line and the step 1 command.
Step 3: Run and check
docker build -t ml-train:dev .
docker run --rm ml-train:dev pip check
docker run --rm --gpus all ml-train:devpip check confirms that every installed package's declared requirements are satisfied. A failure here means the lock file is internally inconsistent. The last line runs training with the host GPUs exposed. That requires the NVIDIA driver and the NVIDIA Container Toolkit on the host. The CUDA runtime that your PyTorch wheel carries must be supported by the host driver. A driver that is too old fails at startup, while a newer driver is fine.
The whole workflow as a script
Putting the three steps in one script means everyone, and CI, runs the same commands.
#!/usr/bin/env bash
set -euo pipefail
IMAGE=ml-train:dev
PY_IMAGE=python:3.11-slim # use the digest form in a real project
# 1. Resolve inside the base image
docker run --rm -v "$PWD":/work -w /work "$PY_IMAGE" \
sh -c "pip install --quiet pip-tools && pip-compile --generate-hashes --quiet -o requirements.lock requirements.in"
# 2. Build from the lock file
docker build -t "$IMAGE" .
# 3. Check consistency, then run
docker run --rm "$IMAGE" pip check
docker run --rm --gpus all "$IMAGE"Day to day you only need steps 2 and 3. Rerun step 1 when you change requirements.in or deliberately update dependencies. If rerunning step 1 changes the lock file when you did not touch requirements.in, something unpinned is still leaking in, and that is worth investigating.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `THESE PACKAGES DO NOT MATCH THE HASHES` | Lock file compiled on a different platform or Python version than the image | Recompile inside the base image used by the Dockerfile |
| `Hashes are required in --require-hashes mode` | A requirement is missing from the lock file, or is a direct URL or editable install | Add it to `requirements.in` and recompile |
| Dependencies reinstall on every code edit | Source copied before the install step, or the lock file is not copied alone | Copy the lock file first, install, then copy source |
| `torch.cuda.is_available()` is `False` in the container | Missing `--gpus all`, missing Container Toolkit, or a driver older than the wheel's CUDA runtime | Check `nvidia-smi` on the host, then inside the container |
| Same lock file, different result next month | Base image referenced by tag, so the OS or Python changed | Pin the base image by digest |
| Training results differ between identical images | Runtime randomness, not the environment | Set seeds and deterministic options, covered later |
Where this workflow stops
An identical image removes the environment as a cause of different results. It does not make training deterministic. GPU kernels, thread scheduling, and data order can still vary between runs. The host GPU driver and the hardware model also differ between machines. Record the driver version and GPU model in your run logs, because the image cannot capture them.
Exercise
A teammate compiles requirements.lock on a Mac, commits it, and the Docker build on a Linux CI runner fails with a hash mismatch. Explain why this happens and give the fix.
Show answer
Many packages ship different wheels per platform and Python version, so the versions and hashes chosen on macOS may not exist for Linux, and any Linux wheel has a different hash. The lock file describes one platform. The fix is to compile the lock file inside the same base image the Dockerfile uses (for example with docker run ... pip-compile --generate-hashes), then commit that file and rebuild.
Exercise
Your image is built from a lock file and a base image referenced as python:3.11-slim. Two builds six months apart give different results even though requirements.lock is unchanged. Which part is unpinned, and how do you pin it?
Show answer
The base image. The tag 3.11-slim is a moving label, so the Python patch version and system libraries can change. Pin it by digest (python:3.11-slim@sha256:<digest>) in both the Dockerfile and the lock-compilation step.