Want to learn how to deploy a machine learning model with Docker and MLflow? This step-by-step tutorial takes you from a trained model to a live API endpoint โ using MLflow for tracking, Flask for serving, and Docker for containerization.
This complete deploy a machine learning model with Docker and MLflow guide is designed for ML engineers who know how to train models but haven’t deployed one to production yet. No prior Docker or web framework experience required.
Table of Contents
01 Why Deploy a Machine Learning Model with Docker and MLflow?
You trained a model. It hits 94% accuracy. You're excited. Then someone asks the question that stops every new ML engineer cold: "How do we actually use it?"
Training a model is step one. Deploying it โ making it available to other systems, APIs, or end users โ is an entirely different skill. Without a repeatable deployment process, your model lives forever in a Jupyter notebook that only runs on your laptop.
This tutorial shows you how to deploy a machine learning model with Docker and MLflow step by step. You'll use MLflow to track and package your model, Flask to serve predictions, and Docker to containerize everything so it runs identically on any machine.
โ
Log with MLflow
โ
Flask API
โ
Docker
โ
Live endpoint
About 45 minutes to complete this deploy a machine learning model with Docker and MLflow tutorial.
02 Prerequisites
- ๐ Python 3.8+ โ check with
python --version - ๐ณ Docker Desktop โ free at docker.com
- ๐ง Basic ML knowledge โ you know what training a model means
- ๐ฆ Python packages โ run
pip install mlflow scikit-learn flask gunicorn
Make sure Docker Desktop is open and shows "Running". The most common mistake when learning how to deploy a machine learning model with Docker and MLflow is running Docker commands while the daemon is still sleeping.
03 Train and Log Model with MLflow
Create train.py. This is the first step in learning how to deploy a machine learning model with Docker and MLflow โ you need a trained model to deploy.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import mlflow import mlflow.sklearn from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score mlflow.set_experiment("iris-deployment") with mlflow.start_run(): X, y = load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) mlflow.log_param("n_estimators", 100) mlflow.log_param("random_state", 42) mlflow.log_metric("accuracy", accuracy) mlflow.sklearn.log_model(model, "model") print(f"Model accuracy: {accuracy:.4f}") print(f"Run ID: {mlflow.active_run().info.run_id}") |
Run: python train.py
04 Find Your MLflow Model Path
|
1 |
find mlruns -name "MLmodel" -print |
Your MODEL_PATH looks like: mlruns/0/abc123def456/artifacts/model
05 Create a Flask API
Create app.py to serve your model:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import numpy as np from flask import Flask, request, jsonify import mlflow.pyfunc app = Flask(__name__) MODEL_PATH = "mlruns/0/YOUR_RUN_ID_HERE/artifacts/model" model = mlflow.pyfunc.load_model(MODEL_PATH) @app.route("/health", methods=["GET"]) def health(): return jsonify({"status": "healthy"}) @app.route("/predict", methods=["POST"]) def predict(): data = request.get_json(force=True) features = np.array(data["features"]).reshape(1, -1) prediction = model.predict(features) return jsonify({"prediction": int(prediction[0])}) if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) |
Create requirements.txt:
|
1 2 3 4 5 |
flask==3.0.0 mlflow==2.11.0 scikit-learn==1.4.0 numpy==1.26.4 gunicorn==21.2.0 |
06 Write the Dockerfile
|
1 2 3 4 5 6 7 8 9 |
FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY app.py . COPY mlruns/0/YOUR_RUN_ID_HERE/artifacts/model ./model EXPOSE 5000 ENV MODEL_PATH=./model CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "2", "app:app"] |
07 Build and Run the Container
|
1 2 3 4 |
docker build -t ml-model-api:v1 . docker run -p 5000:5000 --name iris-api -d ml-model-api:v1 docker ps docker logs iris-api |
08 Test Your API
|
1 2 |
curl http://localhost:5000/health curl -X POST http://localhost:5000/predict -H "Content-Type: application/json" -d '{"features": [5.1, 3.5, 1.4, 0.2]}' |
Expected response: {"prediction": 0} โ your model is now successfully deployed!
09 Troubleshooting Common Errors
docker run -p 5001:5000 ml-model-api:v1 and test at port 5001.find mlruns -name "MLmodel" to get the correct path, then update Dockerfile and rebuild.10 What to Do Next
๐ External resources: MLflow Docs โข Docker Docs โข Flask Docs
๐ MLflow vs W&B โข MLflow vs ClearML