Master Docker for MLOps by containerizing your ML pipeline. Learn to write production-ready Dockerfiles, manage dependencies, and ensure consistent deployment.
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.
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.
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"]
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.-slim or alpine images. Smaller images pull faster during deployment and reduce your attack surface.COPY .env files or API keys into the image. Use environment variables injected at runtime.Once you have your Dockerfile, build it locally to ensure your dependencies are resolved correctly:
Bashdocker build -t my-ml-pipeline:v1 .
After the build, run the container to verify it starts and exposes the endpoint:
Bashdocker 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):
Bashcurl -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.
requirements.txt from your local environment using pip freeze > requirements.txt.Dockerfile in your project root using the example above as a template.requirements.txt. Always use package==1.2.3 instead of just package.root. For production, add a non-privileged user in your Dockerfile (USER appuser) to enhance security.docker buildx to build for specific platforms if your local hardware differs from your cloud target.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.
Master environment parity in your ML pipelines. Learn how to use virtual environments, containerization, and secure config management to avoid deployment drift.
Read moreLearn how to finalize your ML pipeline for production. We cover final validation, dependency locking, and operational readiness for a seamless deployment.
Containerization Basics