Sub topic 15
When to capture the environment in a container
This chapter assumes you have pinned dependencies in a requirements.txt file and can run a notebook from the command line with jupyter nbconvert --execute. It also assumes you have seen basic Docker commands. If not, treat the commands below as a recipe to copy.
What a pinned environment leaves out
A virtual environment with pinned packages fixes the Python packages. It does not fix everything the analysis depends on:
- The Python interpreter itself. A
requirements.txtfile does not record it. - Operating system libraries, such as the C libraries behind some scientific packages.
- Command-line tools the notebook calls, such as
ffmpegorpandoc. - The CPU architecture and the OS version.
A container is a packaged filesystem and process that runs on top of the host's kernel. An image is the saved template a container starts from. An image bundles the interpreter, system libraries, and packages, so the same image behaves the same on a laptop, a server, or a CI runner.
A minimal image for a notebook
This Dockerfile builds on a specific Python version and installs the pinned packages. Your requirements.txt must also list jupyter, nbconvert, and ipykernel. Otherwise the command at the end fails.
FROM python:3.12-slim
WORKDIR /project
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["jupyter", "nbconvert", "--to", "notebook", "--execute", "analysis.ipynb", "--output", "analysis_executed.ipynb", "--output-dir", "outputs"]The requirements.txt file is copied and installed before the rest of the project. Docker caches each step, so editing a notebook does not trigger a reinstall of every package.
Build the image and run it. The -v option mounts the host's outputs folder, so the executed notebook is saved outside the container.
docker build -t analysis:1.0 .
docker run --rm -v "$PWD/outputs:/project/outputs" analysis:1.0Choosing whether to add one
A container adds work. You must build images, store them, and update them for security fixes. For a small analysis that uses only pure Python packages, a pinned virtual environment is often enough. A container pays off in these cases:
- The notebook depends on system libraries or external tools.
- Several people on different operating systems must get identical results.
- The analysis runs on a server or in CI, and you want that run to match your laptop.
- You must be able to rerun the work years later, when today's package downloads may be gone.
Common errors
- Floating tags.
FROM python:3orpython:latestchanges over time. Use a full version tag such aspython:3.12-slim. For strict reproducibility, use the image digest. - Unpinned installs inside the image. Running
pip install pandasin the Dockerfile gives you whatever version is current at build time. Install from the pinned file instead. - Data baked into the image. Large inputs make images heavy. Mount the data at run time and keep the checksum check from the data provenance chapter in the notebook.
- Treating the image as the only record. Keep the Dockerfile and
requirements.txtin Git. Rebuilding from them must still work. - Assuming containers fix randomness. They do not. Keep your seeds.
Exercise
Your notebook uses only pandas and matplotlib, you are the only user, and you run it on one laptop. Your colleague's notebook calls the pandoc command-line tool and runs on a CI server. Which one benefits more from a container, and why?
Show answer
Your colleague's. Pandoc is a system tool, and a pinned requirements.txt cannot record it. A container fixes the tool's version and makes the CI run match local runs. Your notebook is covered by a pinned virtual environment.