ML Notes
Containerizing ML models and workflows using Docker for reproducibility, scalability, and deployment.
Docker enables reproducible, scalable ML deployments by packaging models, dependencies, and configurations into containers.
Why Docker for ML?
Problems Docker solves:
- Dependency hell: "Works on my machine" syndrome
- Reproducibility: Exact same environment everywhere
- Scalability: Easy to deploy multiple instances
- Isolation: Different projects don't interfere
- Consistency: Dev, staging, prod identical
Creating ML Docker Images
Basic Dockerfile
requirements.txt
Building & Running
# Build image
docker build -t ml-model:1.0 .
# Run container
docker run -p 8000:8000 ml-model:1.0
# Run with environment variables
docker run -p 8000:8000 \
-e MODEL_PATH="/models/custom.pkl" \
ml-model:1.0
# Interactive mode
docker run -it ml-model:1.0 bash
# Mount local directory
docker run -v /local/data:/app/data ml-model:1.0ML Model Serving with Flask
Docker Compose for Complete ML Stack
# docker-compose.yml
version: '3.8'
services:
# ML Model API
ml-api:
build: ./ml-service
ports:
- "8000:8000"
environment:
- MODEL_PATH=/models/model.pkl
- LOG_LEVEL=INFO
volumes:
- ./models:/models
- ./logs:/app/logs
depends_on:
- redis
- postgres
restart: unless-stopped
# Cache layer
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
restart: unless-stopped
# Database for predictions
postgres:
image: postgres:14-alpine
environment:
- POSTGRES_DB=ml_predictions
- POSTGRES_USER=mluser
- POSTGRES_PASSWORD=secure_password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
# Monitoring
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
restart: unless-stopped
# Visualization
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
depends_on:
- prometheus
restart: unless-stopped
volumes:
redis_data:
postgres_data:Multi-stage Build for Optimization
Testing Docker Locally
Deploying to Docker Registry
# Tag for Docker Hub
docker tag ml-model:latest username/ml-model:1.0
# Push to registry
docker push username/ml-model:1.0
# Pull from registry
docker pull username/ml-model:1.0
# Run from registry
docker run -p 8000:8000 username/ml-model:1.0
# AWS ECR
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin <account>.dkr.ecr.us-east-1.amazonaws.com
docker tag ml-model:latest <account>.dkr.ecr.us-east-1.amazonaws.com/ml-model:latest
docker push <account>.dkr.ecr.us-east-1.amazonaws.com/ml-model:latestRunning ML Training in Docker
# Run training
docker run \
-v $(pwd)/models:/models \
-e DATA_PATH=/app/data \
ml-train:latest
# Check output
ls -lh models/model.pklHealth Checks in Docker
# Docker will automatically restart unhealthy containers
docker ps # Shows (healthy) or (unhealthy) statusBest Practices
# ✓ Good: Minimal base image
FROM python:3.9-slim
# ✓ Good: Specific versions
RUN pip install numpy==1.21.0
# ✗ Bad: Large base image
FROM ubuntu:20.04
# ✗ Bad: Latest versions (non-deterministic)
RUN pip install numpy
# ✓ Good: Multi-stage build
FROM python:3.9 as builder
# ... build dependencies ...
FROM python:3.9-slim
COPY --from=builder /build /app
# ✓ Good: Non-root user
RUN useradd -m mluser
USER mluser
# ✓ Good: EXPOSE port
EXPOSE 8000
# ✗ Bad: Running as root (security issue)Troubleshooting
# Check image layers
docker history ml-model:latest
# View image size
docker images | grep ml-model
# Inspect running container
docker inspect <container-id>
# Check resource usage
docker stats
# Remove dangling images
docker image prune
# Build without cache
docker build --no-cache -t ml-model:latest .
# Debug by running interactive shell
docker run -it ml-model:latest bash
python -c "import your_module; print(your_module.__file__)"Summary
Docker for ML:
- Ensures reproducibility
- Enables scalability
- Simplifies deployment
- Isolates environments
- Integrates with orchestration (Kubernetes)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Docker for Machine Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, mlops, docker, for, docker for machine learning
Related Machine Learning Topics