Stop 17 · MLOps & Model DeploymentTopics · 2/10

Docker for ML Models

Part of the AI Engineer 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.

Packaging a model and its dependencies into a container so it runs identically across dev, staging and production — solves the classic 'works on my machine' problem for ML services.

The blob zips up a neatly packed suitcase filled with jars, folded clothes and a small box.

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

Base image
Install deps
Copy model
Run server

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

dockerfile
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

~30 min

Quick check

Why copy requirements.txt and install before copying main.py?

Go deeper

  1. 1Containerize a model-serving API with a Dockerfile.
  2. 2Learn to keep image size manageable (multi-stage builds, minimal base images) for faster deploys.
  3. 3Run the container locally and confirm behavior matches your local dev environment.

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.

Download
InsideEdge