0Pricing
Learn AI with Python · Lesson

Containerizing ML Models with Docker

Multi-stage Dockerfile, CUDA base images, model artifact COPY, health endpoints.

Containerizing ML Models with Docker is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Containerize ML Models

Machine learning models depend on a specific Python version, system libraries, and pinned package versions. Docker packages all of this into a single immutable image so the model runs identically on a laptop, a CI runner, and a production cluster.

For ML the biggest pain is dependency drift: a model trained against numpy 1.24 can silently misbehave on numpy 2.0. A container freezes those versions.

The Naive Single-Stage Image

A first attempt installs everything in one stage. It works, but it ships build tools (compilers, caches, dev headers) into the final image, making it large and a bigger attack surface.

FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "serve.py"]

Multi-Stage Builds: The Idea

A multi-stage build uses one stage to compile and install dependencies (the builder), then copies only the finished artifacts into a clean final stage. Build-only tooling never reaches production.

  • Builder stage: full base image, runs pip install
  • Final stage: slim base image, copies installed packages

Builder Stage: pip install

The builder installs packages into a known prefix so they are easy to copy out. Using --user places them under /root/.local, a single directory we can grab later.

FROM python:3.11 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user --no-cache-dir -r requirements.txt

Final Slim Stage

The final stage starts from python:3.11-slim and copies the installed packages from the builder. The slim image omits build toolchains, cutting image size by hundreds of megabytes.

FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH
COPY . .

Copying Model Weights

Large model weights should be copied as their own layer so Docker can cache them. If only your code changes, the weight layer is reused and rebuilds are fast.

COPY model/ /app/model/
COPY src/ /app/src/

EXPOSE the Serving Port

Inference servers (FastAPI, Triton, TorchServe) listen on a port. EXPOSE 8000 documents that the container serves on port 8000. It does not publish the port by itself; you still map it with -p 8000:8000 at run time.

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

HEALTHCHECK Basics

Orchestrators need to know if the model server is alive. A HEALTHCHECK instruction runs a command on an interval; a non-zero exit marks the container unhealthy so it can be restarted or pulled from the load balancer.

HEALTHCHECK with curl

A common pattern hits a lightweight /health endpoint. curl -f fails (non-zero exit) on any HTTP error, which is exactly what HEALTHCHECK expects.

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD curl -f http://localhost:8000/health || exit 1

A Minimal Health Endpoint

Your serving app should expose a fast endpoint that confirms the model is loaded, without running a full inference.

from fastapi import FastAPI

app = FastAPI()
model = None  # loaded at startup

@app.get("/health")
def health():
    return {"status": "ok", "model_loaded": model is not None}

The Complete Dockerfile

Putting it all together: a builder stage installs dependencies, a slim final stage copies them plus the model, exposes 8000, and declares a health check.

FROM python:3.11 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user --no-cache-dir -r requirements.txt

FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH
COPY model/ /app/model/
COPY src/ /app/src/
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
  CMD curl -f http://localhost:8000/health || exit 1
CMD ["uvicorn", "src.serve:app", "--host", "0.0.0.0", "--port", "8000"]

Quick Check

Test your understanding of multi-stage builds.

Recap

You learned to containerize an ML model with a production-grade Dockerfile:

  • Multi-stage build: builder installs deps, slim final stage copies them
  • EXPOSE 8000 documents the serving port
  • HEALTHCHECK CMD curl lets orchestrators detect dead containers
  • Copy weights as their own cacheable layer

Next, you will deploy a containerized model to a managed cloud service.

Frequently asked questions

Is the “Containerizing ML Models with Docker” lesson free?

Yes — the full text of “Containerizing ML Models with Docker” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Containerizing ML Models with Docker”?

Multi-stage Dockerfile, CUDA base images, model artifact COPY, health endpoints. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Containerizing ML Models with Docker” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Containerizing ML Models with Docker
  2. Cloud Deployment: AWS SageMaker
  3. High-Performance Serving with Triton Inference Server
  4. Scaling and Auto-Scaling Model Endpoints
← Back to Learn AI with Python