Docker for MLOps¶
Introduction¶
Learn how Docker helps package model-serving code, dependencies, and model artifacts into reproducible inference containers.
Before You Start¶
You need a serving application, dependency file, and a decision about how the model artifact enters the image. Small teams often bake the approved model into the image; larger teams may download approved versions at startup.
Project Structure¶
ml-api/
├── app.py
├── requirements.txt
├── models/churn.pkl
└── Dockerfile
Step-by-Step Deployment¶
Dockerfile example:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
COPY models/ ./models/
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
Build and run:
docker build -t ml-api:churn-17 .
docker run --rm -p 8000:8000 ml-api:churn-17
Successfully tagged ml-api:churn-17
Uvicorn running on http://0.0.0.0:8000
Testing the Deployment¶
Test the container:
curl -s http://localhost:8000/health
{"status":"ok","model_version":"churn:17"}
Production Considerations¶
Pin dependency versions, scan images, set non-root users, keep images small, publish immutable tags, and include model version metadata.
Common Mistakes¶
- Using
latestfor production deployments. - Building images from unpinned dependencies.
- Copying training data into the serving image.
- Running containers as root unnecessarily.
Related Guides¶
Summary¶
A reliable model deployment is versioned, testable, observable, and reversible.