Sub topic 14
One workflow from lock file to running container
Assume you know basic Python, pip, the command line, Git, and how to run a model with PyTorch or scikit-learn. This chapter connects dependency locking, container building, and execution into one repeatable workflow.
Start with direct dependencies
A dependency is a package your project needs. A direct dependency is one you choose, such as torch. A transitive dependency is a package required by another package.
Declare direct dependencies in requirements.in:
numpy
scikit-learn
torchThis file is readable, but it does not fully define the environment. Package versions and transitive dependencies can change over time.
Generate and use a lock file
A lock file records the resolved dependency tree. It specifies the exact versions that installation should use.
With pip-tools, generate a hash-pinned lock file:
python -m pip install pip-tools
pip-compile --generate-hashes \
--output-file=requirements.lock \
requirements.in
python -m pip install --require-hashes -r requirements.lockA hash is a fingerprint of a package artifact. The --require-hashes option makes pip reject an artifact whose fingerprint is missing or different. Commit both requirements.in and requirements.lock.
You can use uv instead:
uv lock
uv sync --lockedThe --locked option fails when pyproject.toml and uv.lock disagree. That failure is useful because it prevents an accidental, unrecorded dependency change.
Install the lock file in the image
A container image packages an application with its user-space operating system files and dependencies. Copy the lock file before the source code. This lets Docker reuse the dependency layer when only application code changes.
FROM python:3.11-slim
WORKDIR /app
COPY requirements.lock .
RUN python -m pip install --no-cache-dir \
--require-hashes -r requirements.lock
COPY src/ ./src/
CMD ["python", "-m", "src.train"]For a release, pin FROM to a verified digest, such as python:3.11-slim@sha256:<digest>. A tag can move to a different image later. A digest identifies one image manifest.
Build, run, and check the workflow
docker build -t ml-train:dev .
docker run --rm ml-train:devFor a GPU workload, use the NVIDIA Container Toolkit on the host:
docker run --rm --gpus all ml-train:devThe host NVIDIA driver communicates with the container. The image supplies CUDA user-space libraries. The driver must support the CUDA version expected by the framework in the image.
Verify in CI
CI should regenerate the lock file and fail if Git detects a difference. It should also rebuild the image:
pip-compile --generate-hashes -o requirements.lock requirements.in
git diff --exit-code requirements.lock
docker build -t ml-train:ci .Update dependencies deliberately: edit the direct declarations, regenerate the lock file, run tests, rebuild the image, and review the dependency diff before committing.
Exercise
Why does the Dockerfile copy requirements.lock before src/?
Show answer
Docker can reuse the dependency-installation layer when source code changes, so rebuilds are faster while the locked environment stays unchanged.