Docker for ML Models
Part of the Machine Learning Roadmap
In short
'It runs on my laptop' doesn't help when the server has a different Python and a different scikit-learn. Docker packs your model, code and exact libraries into one box that runs the same everywhere.
Docker packages your model, code, Python version and libraries into one container image that runs the same everywhere. It removes the 'works on my machine' problem when deploying ML services.

Think of it like this
Packing for the hostel
Before you leave for the hostel, Mummy packs everything: clothes, medicines, the exact pickle you like. You don't hope the hostel has it. A Docker image is that suitcase: your model, code, Python and libraries, packed together, so nothing is missing when it lands on a server.
Watch it happen
Building the image, layer by layer
1/4Start from python:3.12-slim, a small official image with Python already installed.
The big ideas
What you need to know
Pin versions
A model pickled with one scikit-learn version may break on another. Pin exact versions in requirements.txt.
Layer order matters
Install dependencies before copying code, so small code changes don't reinstall everything.
Keep images slim
Use slim base images, --no-cache-dir and a .dockerignore so datasets and notebooks don't sneak in.
GPUs need extra setup
GPU images start from CUDA base images and need the NVIDIA container toolkit on the host.
See it in code
Walk through the code
1FROM python:3.12-slim2 3WORKDIR /app4COPY requirements.txt .5RUN pip install --no-cache-dir -r requirements.txt6 7COPY spam_model.joblib main.py ./8EXPOSE 80009CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]1/4A small official Python base image.
A Dockerfile for the FastAPI model server. Build with docker build -t spam-api . and run with docker run -p 8000:8000 spam-api.
Save yourself the pain
Common mistakes
Oops
Running uvicorn on 127.0.0.1 inside the container.
Do this instead
Use --host 0.0.0.0 so the port you publish with -p actually reaches the server.
Oops
Copying the whole project folder, including a 3 GB dataset.
Do this instead
Add a .dockerignore and copy only the files the server needs.
Your mission
Containerise your model API
Quick check
Why copy requirements.txt and install before copying main.py?
Go deeper
- 1Write a Dockerfile for your FastAPI model service and build the image.
- 2Use a slim Python base image and pin package versions.
- 3Run the container locally and call the /predict endpoint.
Also part of these roadmaps
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.