Stop 12 · Model Deployment & MLOpsTopics · 3/8

Docker Basics for ML

Part of the Data Science Roadmap

In short

'It works on my laptop' is the most common deployment excuse. Docker packs your code, libraries and model into one image that runs the same way on any machine.

Packaging your model API and its dependencies into a portable container that runs identically anywhere — solves the classic 'it works on my machine' problem for ML deployments specifically.

The mascot packs a steel tiffin carrier with every compartment full, plus a napkin and pickle pot.

Think of it like this

A fully packed tiffin

A well-packed tiffin has everything: dal, rice, roti, pickle, even a spoon. Open it in the office, on a train or at the park and lunch works the same. A Docker image is a tiffin for your app, everything included.

Watch it happen

From code to running container

Dockerfile
Build
Image
Run
Anywhere

1/5A Dockerfile lists the steps: base Python image, install libraries, copy code and model.

The big ideas

What you need to know

Image vs container

An image is the recipe snapshot. A container is a running copy of it. One image can start many containers.

Layers are cached

Copy requirements.txt and install before copying code, so rebuilding after a code change is quick.

Use slim bases

python:3.12-slim is much smaller than the full image. Smaller images build, push and start faster.

No secrets inside

Pass API keys as environment variables at run time. Never bake them into the image.

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 main.py iris_model.joblib ./8EXPOSE 80009CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

1/4Start from a small official Python image.

Packages the FastAPI model server. Build with docker build -t iris-api . and run with docker run -p 8000:8000 iris-api.

Save yourself the pain

Common mistakes

Oops

Running uvicorn on 127.0.0.1 inside the container.

Do this instead

Bind to 0.0.0.0 inside containers, or the server only accepts connections from within the container itself.

Oops

Copying the whole project folder, datasets and .venv included.

Do this instead

Add a .dockerignore for data, virtual environments and notebooks to keep the image small.

Your mission

Containerise your API

~35 min

Quick check

Why copy requirements.txt and install packages before copying your code?

Go deeper

  1. 1Write a Dockerfile that packages your model API from the previous topic.
  2. 2Build and run the container locally, confirming it behaves the same as your local setup.
  3. 3Learn why pinning exact library versions matters even more for ML than typical software.

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