12 Containerisation
12.1 Containerisation with Docker
As your data science projects grow more complex, you may encounter the “it works on my machine” problem — where code runs differently in different environments. Containerisation solves this by packaging your code and its dependencies into a standardised unit called a container. Building on the Reproducible Environments chapter (uv and conda for Python, renv for R), containers extend that isolation to the whole runtime — the operating system libraries, the system tools, and the language version itself, not just the packages.
12.1.1 What a Container Buys You
An environment file pins your packages. A container pins the machine around them, so the same image runs the same way on your laptop, a colleague’s, and a server you’ve never logged into. That is what makes it the unit deployment platforms accept: you hand over the image rather than a list of things to install and hope for.
Think of containers as lightweight, portable units that package everything your code needs to run. Unlike virtual machines, containers don’t boot their own operating system; they share the host’s kernel — the core of the operating system that manages memory, files, and processes — and isolate themselves using features that kernel already provides. That makes them much faster to start and much cheaper in memory than a VM, while still giving you a reproducible, isolated environment.
12.1.2 Installing Docker
Docker is the most widely used containerisation platform, and its command-line tool (docker) is effectively the lingua franca of containers. Even the alternative tools below aim to be drop-in replacements for it. We’ll use Docker Desktop in this chapter, but note the following licensing caveat before installing:
Since 2021, Docker Desktop has required a paid subscription for commercial use above a size threshold — as of writing, more than 250 employees or over $10 million in annual revenue. It remains free for personal use, education, smaller businesses, and open-source projects. Thresholds like this get revised, so check Docker’s current terms rather than this paragraph before concluding you’re under it. If your workplace falls above that threshold, the free alternatives below all run the same images and Dockerfiles:
- Rancher Desktop: open source, works on Windows/macOS/Linux, bundles Kubernetes
- Podman Desktop: open source, Red Hat–maintained, and the closest drop-in replacement for the Docker CLI. It runs without a background service (a daemon) holding root privileges, which some IT departments prefer
- OrbStack (macOS only): very fast, polished; free for personal use, paid for commercial
The Dockerfiles in this chapter build on each of these tools, and most of the docker commands carry over. Compose and the ownership of files in mounted folders are where they differ, so check the tool’s own documentation for those two.
12.1.2.1 On Windows
- Download Docker Desktop for Windows (or one of the alternatives above)
- Run the installer and follow the prompts
- Windows 10 Home users should ensure WSL 2 is installed first
12.1.2.2 On macOS
- Download Docker Desktop for Mac (or OrbStack / Rancher Desktop)
- Run the installer and follow the prompts
12.1.2.3 On Linux
# Ubuntu. docker.io alone has no `docker compose`, which this chapter uses.
# On Debian the Compose package is called docker-compose instead.
sudo apt update
sudo apt install docker.io docker-compose-v2 docker-buildx
sudo systemctl enable --now docker
# Add your user to the docker group to run Docker without sudo
sudo usermod -aG docker $USER
# Log out and back in for this to take effect12.1.2.4 Verifying Installation
Open a terminal and run:
docker --version
docker run hello-worldIf both commands complete successfully, Docker is installed correctly.
12.1.3 Docker Fundamentals
Before creating our first data science container, let’s understand some Docker basics:
- Images: Read-only templates that contain the application code, libraries, dependencies, and tools
- Containers: Running instances of images
- Dockerfile: A text file with instructions to build an image
- Docker Hub: A registry of pre-built Docker images
- Volumes: Persistent storage for containers
The relationship between these components works like this: you create a Dockerfile that defines how to build an image, the image is used to run containers, and volumes allow data to persist beyond the container lifecycle.
12.1.4 Creating Your First Data Science Container
Let’s create a basic data science container using a Dockerfile:
- Create a new directory for your project:
mkdir docker-data-science
cd docker-data-science- Create a file named
Dockerfilewith the following content:
# Use a base image with Python installed
FROM python:3.13-slim
# Set working directory
WORKDIR /app
# Copy requirements file
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Create a non-root user and give it the working directory
RUN useradd --create-home --shell /bin/bash jovyan && chown jovyan:jovyan /app
# Copy the rest of the code, owned by the non-root user
COPY --chown=jovyan:jovyan . .
USER jovyan
# Command to run when the container starts
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--port=8888", "--no-browser"]- Create a
requirements.txtfile with your Python dependencies:
numpy
pandas
matplotlib
scipy
scikit-learn
jupyter
jupyterlab
- Build the Docker image:
docker build -t data-science-env .This command tells Docker to build an image based on the instructions in the Dockerfile and tag it with the name “data-science-env”. The . at the end specifies that the build context is the current directory.
- Run a container from the image:
docker run -p 8888:8888 -v "$(pwd)":/app data-science-envThis command does two important things:
- Maps port 8888 in the container to port 8888 on your host machine, allowing you to access Jupyter Lab in your browser
- Mounts your current directory to
/appin the container, so changes to files are saved on your computer
"$(pwd)" is how macOS, Linux and WSL shells write “the current directory”, and the quotes keep a path with spaces in one piece. The other Windows shells spell it differently:
| Shell | Mount option |
|---|---|
| macOS, Linux, WSL | -v "$(pwd)":/app |
| Git Bash | -v "$(pwd)":/app, with MSYS_NO_PATHCONV=1 in front of docker run |
| PowerShell | -v "${PWD}:/app" |
| Command Prompt | -v "%cd%":/app |
Git Bash rewrites anything that looks like a Unix path before Docker sees it, turning /app into a folder under C:\Program Files\Git. The container still starts, with none of your files in /app. MSYS_NO_PATHCONV=1 switches that rewriting off for the one command:
MSYS_NO_PATHCONV=1 docker run -p 8888:8888 -v "$(pwd)":/app data-science-env- Open the Jupyter Lab URL shown in the terminal output
You now have a containerised data science environment that can be shared with others and deployed to different systems.
12.1.5 Understanding the Dockerfile
Let’s break down the Dockerfile we just created:
# Use a base image with Python installed
FROM python:3.13-slimThe FROM statement specifies the base image to use. We’re starting with a lightweight Python 3.13 image. Always pick a currently-supported Python version; consult the official release schedule before pinning in production, since each version’s support window eventually ends.
# Set working directory
WORKDIR /app
# Copy requirements file
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txtRUN executes a command while the image is being built. The requirements file is copied and installed on its own, before the rest of the code, because Docker caches each step: when only your code changes, a rebuild reuses the layer with the packages already installed. No compiler is needed for this list, since every package on it publishes prebuilt wheels for Linux.
# Create a non-root user and give it the working directory
RUN useradd --create-home --shell /bin/bash jovyan && chown jovyan:jovyan /app
COPY --chown=jovyan:jovyan . .
USER jovyanuseradd creates the user, and chown hands it /app, which WORKDIR created as root. COPY . . copies everything from the current directory on the host into the working directory, --chown sets the owner of each file as it copies, and USER jovyan means every command after this line, including the CMD below, runs as that user. Without the chown, Jupyter could open the copied notebooks but not create a new one.
By default everything in a container runs as root, which is fine until the container is exposed to the network or mounts a host directory, at which point a process that escapes its intended boundary is doing so with root privileges. Creating an unprivileged user costs two lines. (jovyan is not a special name. It’s the convention the official Jupyter images use, and following it keeps file ownership consistent if you later switch to one of those base images.)
Ownership set in the Dockerfile applies only to what’s baked into the image. The -v mount in the run command lays a directory from your host over /app, hiding whatever the image had at that path, and the container sees the host’s ownership on everything there.
On macOS and Windows, Docker Desktop translates ownership for you and this rarely comes up. On Linux the host’s numeric user ID passes through unchanged, so if your account isn’t UID 1000 the mounted files belong to someone jovyan isn’t, and saving fails with a permission error.
Run the container as yourself instead of rebuilding the image:
docker run -p 8888:8888 -u $(id -u):$(id -g) -e HOME=/tmp -v "$(pwd)":/app data-science-envid -u and id -g are your own user and group numbers, so files the container writes come back owned by you. Your user number has no account inside the image, which leaves Jupyter with / as its home directory and nowhere to write its runtime files; HOME=/tmp gives it one.
# Command to run when the container starts
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--port=8888", "--no-browser"]The CMD statement specifies the command to run when the container starts. In this case, we’re starting Jupyter Lab. We bind to 0.0.0.0 so the server accepts connections from outside the container (otherwise the port mapping from the host wouldn’t reach it).
12.1.6 Using Pre-built Data Science Images
Instead of building your own Docker image, you can use popular pre-built images:
12.1.6.1 Jupyter Docker Stacks
The Jupyter team maintains several ready-to-use Docker images:
# Basic Jupyter Notebook. Pin a specific dated tag for reproducibility
docker run -p 8888:8888 quay.io/jupyter/minimal-notebook:2025-10-13
# Data science-focused image with pandas, matplotlib, etc.
docker run -p 8888:8888 quay.io/jupyter/datascience-notebook:2025-10-13
# All the above plus TensorFlow
docker run -p 8888:8888 quay.io/jupyter/tensorflow-notebook:2025-10-13These pre-built images offer a convenient way to get started without creating your own Dockerfile. The Jupyter Docker Stacks project provides a range of images for different needs, from a bare Python and JupyterLab up to images with the scientific stack, R, and deep learning frameworks already installed.
Two small but important habits:
- Pull from
quay.io/jupyter/*: the project moved off Docker Hub, andquay.iois now the canonical location. - Pin a specific dated tag (e.g.
2025-10-13) rather thanlatest. Usinglatestmeans the image can silently change under you between runs, which defeats the whole point of containers for reproducibility. Check the jupyter/docker-stacks releases page for current tags.
12.1.6.2 RStudio
For R users, the Rocker project maintains RStudio Server images. RStudio needs a password, and anything you type as part of a command is saved in your shell’s history, so type the password at a prompt instead:
# Bash, Git Bash, WSL, macOS. The password isn't shown as you type it
printf 'RStudio password: '; read -rs RSTUDIO_PASSWORD; echo
export RSTUDIO_PASSWORD
docker run -p 8787:8787 \
-e PASSWORD="$RSTUDIO_PASSWORD" \
rocker/rstudio:4.5PowerShell asks through a credential prompt instead, and uses a backtick rather than a backslash to continue a line:
$env:RSTUDIO_PASSWORD = (Get-Credential -UserName rstudio -Message "RStudio password").GetNetworkCredential().Password
docker run -p 8787:8787 `
-e PASSWORD="$env:RSTUDIO_PASSWORD" `
rocker/rstudio:4.5Access RStudio at http://localhost:8787 with username rstudio and the password you set. Pin to a specific Rocker tag (e.g. rocker/rstudio:4.5) rather than using latest, for the same reproducibility reasons as above. Rocker publishes tags on Docker Hub: browse there to pick a specific patch version if you need one.
12.1.7 Docker Compose for Multiple Containers
For more complex setups with multiple services (e.g., Python, R, and a database), Docker Compose allows you to define and run multi-container applications:
- Create a
.envfile alongside your compose file with the secrets, and add.envto your.gitignoreso it never reaches a repository:
RSTUDIO_PASSWORD=a-strong-password-you-chose
POSTGRES_PASSWORD=another-strong-password
- Create a file named
compose.yaml(the newer Compose V2 name;docker-compose.ymlstill works):
services:
jupyter:
image: quay.io/jupyter/datascience-notebook:2025-10-13
ports:
- "8888:8888"
volumes:
- ./jupyter_data:/home/jovyan/work
rstudio:
image: rocker/rstudio:4.5
ports:
- "8787:8787"
environment:
- PASSWORD=${RSTUDIO_PASSWORD}
volumes:
- ./r_data:/home/rstudio
postgres:
image: postgres:17
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- ./postgres_data:/var/lib/postgresql/dataThe ${RSTUDIO_PASSWORD} and ${POSTGRES_PASSWORD} placeholders are filled in from your .env file at runtime, so the secrets never live in the file you commit.
- Start all services (Compose V2 uses
docker compose, a space, not a hyphen):
docker compose up- Access Jupyter at http://localhost:8888 and RStudio at http://localhost:8787
Docker Compose creates a separate container for each service in your configuration while allowing them to communicate with each other. This approach makes it easy to run complex data science environments with multiple tools.
12.1.8 Docker for Machine Learning Projects
For machine learning projects, containers are particularly valuable for ensuring model reproducibility and simplifying deployment.
Docker’s COPY fails the build if the source path doesn’t exist, so the Dockerfile below needs this layout in place before it will build:
ml-api/
├── Dockerfile
├── requirements.txt
├── app.py
└── models/
└── model.pkl <- your trained model
If you have no trained model to hand, write a throwaway one so the example builds. Run this once from the ml-api directory, in an environment that has scikit-learn:
# make_model.py
import pathlib
import pickle
import numpy as np
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(np.array([[0.0], [1.0]]), np.array([0.0, 1.0]))
pathlib.Path("models").mkdir(exist_ok=True)
with open("models/model.pkl", "wb") as f:
pickle.dump(model, f)- Create a project-specific Dockerfile:
FROM python:3.13-slim
WORKDIR /app
# Copy and install requirements first for better caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the model artefact and the application that serves it
COPY models/ models/
COPY app.py .
# Serve as an unprivileged user (best practice 9 below)
RUN useradd --create-home appuser
USER appuser
# Expose port for API
EXPOSE 5000
# Run the API service under a production web server, not Flask's
# built-in development server (see the Deployment chapter)
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]- Create a
requirements.txtfor the API. This is a different, much smaller set than the notebook environment earlier in the chapter — a serving container needs the web server and whatever library loads your model, and has no use for JupyterLab:
flask
gunicorn
scikit-learn
numpy
pickle stores a reference to the classes that built the object, not a self-contained description of it. Loading a scikit-learn model in a container with a different scikit-learn version produces either a warning about version mismatch or a failure to unpickle at all. Pin the version here to whatever you trained with — scikit-learn==1.9.0 rather than bare scikit-learn — which is one more reason the environment chapter’s advice about recording versions matters.
- Create a simple model serving API (
app.py):
from flask import Flask, jsonify, request
import pickle
import numpy as np
app = Flask(__name__)
# Load pre-trained model
with open('models/model.pkl', 'rb') as f:
model = pickle.load(f)
@app.route('/health')
def health():
# A cheap endpoint for the container runtime to poll - see HEALTHCHECK below
return jsonify({'status': 'ok'})
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
features = np.array(data['features']).reshape(1, -1)
prediction = model.predict(features)[0]
return jsonify({'prediction': prediction.tolist()})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)- Build and run the container:
docker build -t ml-model-api .
docker run -p 5000:5000 ml-model-apiThis creates a containerised API service for your machine learning model that can be deployed to any environment that supports Docker.
12.1.9 Best Practices for Docker in Data Science
To get the most out of Docker for data science, follow these best practices:
Keep images lean: Smaller images pull faster, deploy faster, and ship less software that could turn out to have a vulnerability in it. Use
-slim(or-alpinefor pure-Python workloads) base images when you can.# Roughly a tenth the size of the full python:3.13 image FROM python:3.13-slimA
#starts a comment only at the beginning of a line in a Dockerfile. Put one after an instruction and Docker reads it as more arguments.Alpine-based images use
muslinstead ofglibc, which occasionally breaks scientific Python packages that ship precompiled wheels forglibconly. When in doubt, start with-slim.Use multi-stage builds for production: Separate building dependencies from runtime
# Build stage FROM python:3.13 AS builder WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Runtime stage FROM python:3.13-slim WORKDIR /app COPY --from=builder /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages COPY --from=builder /usr/local/bin /usr/local/bin COPY . . CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]The final image carries the installed packages and your code, and leaves the first stage’s compilers, header files and build tools behind. That saves space when a package has to be compiled during the install. When every requirement ships a prebuilt wheel, as in this chapter’s examples, a single-stage build on
-slimcomes out about the same size. Each tool left out is also one fewer thing to use against you if someone gets into the container.The second
COPY --from=builderline is easy to leave out and the resulting failure is confusing.site-packagesholds the importable library code, but packages that install a command —gunicorn,jupyter,streamlit,dvc— put an executable script in/usr/local/bin, which is a different directory. Copy onlysite-packagesandimport gunicornworks fine whilegunicornat the command line reportsnot found.Layer your Dockerfile logically: Order commands from least to most likely to change
# System dependencies change rarely RUN apt-get update && apt-get install -y gcc # Requirements change occasionally COPY requirements.txt . RUN pip install -r requirements.txt # Application code changes frequently COPY . .Use volume mounts for data: Keep data outside the container
docker run -v /path/to/local/data:/app/data my-data-science-imageImplement proper versioning: Tag images meaningfully
docker build -t mymodel:1.0.0 .Create a .dockerignore file: Exclude unnecessary files
# .dockerignore .git .env .venv/ venv/ __pycache__/ *.pyc data/COPY . .copies whatever is in the folder, including a.envfile, and.gitignorehas no effect on it. Anyone who can pull the image can read a password baked into one of its layers.Use environment variables for configuration (not secrets):
ENV MODEL_PATH=/app/models/model.pklENVvalues are baked into the image and visible to anyone who can pull it, so they are fine for non-sensitive defaults like file paths or log levels. Pass actual secrets at runtime viadocker run -e,--env-file, or your platform’s secret store. Never hard-code them in a Dockerfile.Add a
HEALTHCHECKso the runtime can detect and restart broken containers:HEALTHCHECK --interval=30s --timeout=5s --retries=3 \ CMD python -c "import urllib.request; \ urllib.request.urlopen('http://localhost:5000/health')"Two things have to line up here, and both are easy to get wrong. The port and path must match something your application actually serves —
5000and/healthabove correspond to the Flask API earlier in this chapter; point a healthcheck at a route that doesn’t exist and Docker will report a perfectly healthy container as unhealthy forever. And the command must exist inside the image: healthcheck examples online almost always usecurl, which the-slimimages this chapter recommends don’t include, so the check errors every time it runs. Python is guaranteed to be there, so use Python.Run as a non-root user in production images. Some images ship one, such as
jovyanin the Jupyter stacks andrstudioin Rocker’s RStudio images. The officialpythonimages don’t, so add a user near the end of the Dockerfile:RUN useradd --create-home appuser USER appuserRunning as root inside the container isn’t catastrophic on its own, but combined with volume mounts or a container escape it makes things much worse than they need to be.
12.1.10 Common Docker Commands for Data Scientists
The commands you’ll use day to day:
# List running containers
docker ps
# List all containers (including stopped ones)
docker ps -a
# List images
docker images
# Stop a container
docker stop container_id
# Remove a container
docker rm container_id
# Remove an image
docker rmi image_id
# View container logs
docker logs container_id
# Execute a command in a running container
docker exec -it container_id bash
# Clean up unused resources
docker system prunedocker system prune deserves a particular mention: images and stopped containers accumulate quickly, and reclaiming tens of gigabytes is a common first result.
12.2 Conclusion
A container packages your code, its dependencies and its configuration into one image that someone else can run without rebuilding your setup.
By the end of this chapter you should have:
- Docker installed, with
docker run hello-worldcompleting successfully - An image you built yourself from a Dockerfile, serving Jupyter Lab on
localhost:8888 - A
compose.yamlbringing up more than one service at once, with its passwords read from a.gitignored.envfile rather than written into the file - A rough sense of when a pre-built image (the Jupyter stacks, Rocker) is the better starting point than writing your own
Kubernetes, the tool most often mentioned in the same breath as Docker, runs many containers across many machines. That’s a problem you’re unlikely to have working on your own.