Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Cloud and Containers for ML  ›  Lesson

Docker Images for Models

Docker 14 minA model image freezes the exact Python version, libraries and inference code, so the model runs the same in CI, on a laptop and in production.

Docker Images for Models

The Docker course showed how images package any app. Models add a twist: a saved model is fussy about its environment. A churn model pickled with one version of scikit-learn may refuse to load, or quietly behave differently, under another. The image is how you freeze the environment the model was trained for.

The rule

Pin the environment, bake in a known model, tag it with the version.

Here is a serving image for the churn API:

FROM python:3.11-slim
WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY serve.py .
COPY model/ ./model/

EXPOSE 8000
CMD ["uvicorn", "serve:app", "--host", "0.0.0.0", "--port", "8000"]

And requirements.txt, with exact versions matching the training run:

scikit-learn==1.5.2
pandas==2.2.3
fastapi==0.115.0
uvicorn==0.30.6

The three parts of the rule:

  • Pin. A specific base tag (python:3.11-slim, not latest) and == versions for every library, matching training. Unpinned installs pick up whatever is newest on build day.
  • Bake in a known model. model/ is filled by a build step that fetches a named version from the registry, never whatever file is lying around. Some teams instead load the model at startup from the registry; either way, the version is explicit.
  • Tag. Build as churn-api:7 when it contains model version 7. The image tag and the model version now tell the same story.

Layer order matters for speed: copying requirements.txt and installing before copying the code means a code change does not reinstall every library. Models that need GPUs start from a CUDA-enabled base image instead, which is much larger, so only use one when the model needs it.

The shortcut

Review any model Dockerfile with three "which" questions: which Python? Which libraries? Which model? Each must have one exact answer.

Reviewing a bad DockerfileStep 1 of 4
  1. Which Python?

    FROM python:latest — no exact answer; it moves between builds. Fix: python:3.11-slim.

Drills

Answer fast — trust the rule.

quick drill

Why is FROM python:latest risky for a model image?

quick drill

A model trained with scikit-learn 1.5.2 is served with an unpinned install. What can go wrong?

quick drill

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

quick drill

What is the best tag for an image containing churn-model version 7?

Remember

  • Pin the environment, bake in a known model, tag it with the version.
  • Review with three questions: which Python, which libraries, which model.
  • Trap: latest tags and unpinned installs; they change silently between builds.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainReview the Dockerfile

A churn serving Dockerfile uses FROM python:latest, runs pip install scikit-learn with no version, and copies the model from the developer's Downloads folder at build time. Explain the three problems and how to fix each.