Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 46 of the Intermediate Machine Learning: Real-World Pipelines course
AI/MLJune 26, 20264 min read

Containerization Basics: Packaging ML Pipelines for Deployment

Master Docker for MLOps by containerizing your ML pipeline. Learn to write production-ready Dockerfiles, manage dependencies, and ensure consistent deployment.

DockerMLOpsContainerizationDeploymentPythonInfrastructureaimachine-learning

Previously in this course, we covered Automated Retraining Triggers: MLOps Pipeline Maintenance to keep our models fresh. This lesson shifts focus from the logic of the pipeline to its execution environment, ensuring that the code running on your laptop behaves identically when it hits a production server.

Why Containerization is Essential for MLOps

In machine learning, "it works on my machine" is a dangerous fallacy. Your pipeline relies on specific versions of Python, scikit-learn, pandas, and potentially system-level libraries like libgomp or CUDA drivers. If the production environment drifts—even by a minor version—your model’s numerical output can change, leading to silent failures.

Docker solves this by bundling your application code, runtime, system tools, and libraries into a single, immutable unit called an image. This achieves environment parity, a cornerstone of reliable deployment and modern MLOps.

Writing Your First Pipeline Dockerfile

A Dockerfile is a text document containing instructions for building a container image. For an ML pipeline, we prioritize small image size and security. We generally avoid "latest" tags to ensure reproducibility.

Here is a standard pattern for an inference-ready pipeline:

Dockerfile
# Use a slim Python base image
FROM python:3.10-slim

# Set a working directory
WORKDIR /app

# Install system dependencies if needed (e.g., for lightgbm or xgboost)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first to leverage Docker layer caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the serialized model and pipeline code
COPY models/ ./models/
COPY src/ ./src/

# Expose the API port
EXPOSE 8000

# Command to run your FastAPI/Flask app
CMD ["python", "src/main.py"]

Key Principles for ML Containers

  1. Layer Caching: By copying requirements.txt and running pip install before copying your code, Docker caches the dependency layer. If you change your code, you don't have to re-download the entire environment.
  2. Minimalism: Use -slim or alpine images. Smaller images pull faster during deployment and reduce your attack surface.
  3. No Secrets: Never COPY .env files or API keys into the image. Use environment variables injected at runtime.

Worked Example: Testing Containerized Inference

Once you have your Dockerfile, build it locally to ensure your dependencies are resolved correctly:

Bash
docker build -t my-ml-pipeline:v1 .

After the build, run the container to verify it starts and exposes the endpoint:

Bash
docker run -p 8000:8000 my-ml-pipeline:v1

To test, send a sample payload to your API (assuming you followed Designing Inference APIs: From Pipeline to FastAPI Endpoint):

Bash
curl -X POST http://localhost:8000/predict \
     -H "Content-Type: application/json" \
     -d '{"feature1": 0.5, "feature2": 1.2}'

If the container returns a prediction, your environment is correctly packaged. If it crashes, check your requirements.txt—often, a package that relies on a specific C-library is missing from the system-level apt-get install.

Hands-on Exercise

  1. Audit your environment: Generate a fresh requirements.txt from your local environment using pip freeze > requirements.txt.
  2. Create a Dockerfile: Place a Dockerfile in your project root using the example above as a template.
  3. Build and Run: Execute the build command and verify that your pipeline can load the serialized model (referencing our work in Serializing Pipelines with Joblib for Production Deployment).
  4. Verify: Confirm that the inference endpoint responds correctly to a test request.

Common Pitfalls

  • Bloated Images: Including large datasets or training checkpoints in the Docker image. Keep the image focused on inference; load data/models from external volumes or cloud storage (e.g., S3).
  • Version Drift: Forgetting to pin versions in requirements.txt. Always use package==1.2.3 instead of just package.
  • Root User: Running the container as root. For production, add a non-privileged user in your Dockerfile (USER appuser) to enhance security.
  • Architecture Mismatch: Building an image on an ARM-based Mac (Apple Silicon) and deploying to an x86 server. Use docker buildx to build for specific platforms if your local hardware differs from your cloud target.

Recap

Containerization is the bridge between a working model and a reliable service. By using Docker to create reproducible, versioned images, you ensure that your ML pipeline behaves predictably regardless of where it runs. We’ve moved from raw code to a portable artifact ready for the next stage of our deployment lifecycle.

Up next: We'll explore Handling Environment Parity, focusing on secrets management and configuration strategies to ensure our containers remain secure and flexible across environments.

Previous lessonAutomated Retraining TriggersNext lesson Handling Environment Parity
Back to Blog

Similar Posts

AI/MLJune 26, 20263 min read

Handling Environment Parity: Ensuring ML Pipeline Consistency

Master environment parity in your ML pipelines. Learn how to use virtual environments, containerization, and secure config management to avoid deployment drift.

Read more
AI/MLJune 26, 20263 min read

Project Milestone: Deployment Readiness for ML Pipelines

Learn how to finalize your ML pipeline for production. We cover final validation, dependency locking, and operational readiness for a seamless deployment.

Part of the course

Intermediate Machine Learning: Real-World Pipelines

intermediate · Lesson 46 of 49

  1. 1

    Pipeline Architecture Essentials

    4 min
  2. 2

    ColumnTransformer for Heterogeneous Data

    3 min
  3. 3

    Custom Transformers for Feature Engineering

    3 min
Read more
AI/MLJune 26, 20264 min read

Logging and Observability for Production ML Pipelines

Master production logging and observability to track execution times and build robust audit trails for your ML pipelines. Ensure your models remain debuggable.

Read more
  • 4

    Handling Missing Values Strategically

    4 min
  • 5

    Scaling and Normalization Pipelines

    3 min
  • 6

    Encoding Categorical Variables

    3 min
  • 7

    Feature Selection in Pipelines

    3 min
  • 8

    Data Leakage Prevention Strategies

    4 min
  • 9

    Designing Reproducible Pipelines

    3 min
  • 10

    Project Initialization: Defining the Prediction Problem

    3 min
  • 11

    Introduction to Cross-Validation

    3 min
  • 12

    Stratification for Imbalanced Data

    4 min
  • 13

    Time-Series Validation Strategies

    4 min
  • 14

    Confusion Matrices and Beyond

    4 min
  • 15

    Precision-Recall Curves

    4 min
  • 16

    ROC-AUC Analysis

    3 min
  • 17

    Cost-Sensitive Learning

    4 min
  • 18

    Handling Class Imbalance with Resampling

    3 min
  • 19

    Advanced Metrics for Imbalanced Datasets

    4 min
  • 20

    Project Milestone: Building the Baseline Pipeline

    3 min
  • 21

    Introduction to GridSearchCV

    3 min
  • 22

    RandomizedSearchCV for Efficiency

    3 min
  • 23

    Bayesian Optimization Principles

    3 min
  • 24

    Early Stopping in Iterative Models

    4 min
  • 25

    Managing Computational Resources

    3 min
  • 26

    Hyperparameter Stability Analysis

    4 min
  • 27

    Pipeline Parameter Nesting

    3 min
  • 28

    Project Milestone: Tuning the Champion Model

    3 min
  • 29

    Baseline-to-Champion Framework

    3 min
  • 30

    Statistical Significance in Model Comparison

    3 min
  • 31

    Model Ensembling: Voting and Averaging

    3 min
  • 32

    Stacking Architectures

    4 min
  • 33

    Blending Techniques

    4 min
  • 34

    Interpreting Complex Ensembles

    3 min
  • 35

    Managing Model Complexity

    3 min
  • 36

    Bias-Variance Tradeoff in Ensembles

    4 min
  • 37

    Project Milestone: The Ensemble Strategy

    3 min
  • 38

    Serializing Pipelines with Joblib

    4 min
  • 39

    Versioning Models and Data

    3 min
  • 40

    Designing Inference APIs

    3 min
  • 41

    Input Validation and Schema Enforcement

    4 min
  • 42

    Monitoring Data Drift

    4 min
  • 43

    Tracking Performance Degradation

    3 min
  • 44

    Logging and Observability

    4 min
  • 45

    Automated Retraining Triggers

    4 min
  • 46

    Containerization Basics

    4 min
  • 47

    Handling Environment Parity

    3 min
  • 48

    Documentation for Production

    4 min
  • 49

    Project Milestone: Deployment Readiness

    3 min
  • View full course