How to Build a Docker Image for an MLflow Model
mlflow models build-docker packages a logged model into a Docker image, together with Python, MLflow, the model's own packages and a web server that answers prediction requests. The image runs the same way on any computer with Docker. This guide builds one for an iris classifier and looks at what the build does, with real output from MLflow 3.18.0 and Docker Desktop 4.100.1.
Syntax
mlflow models build-docker -m MODEL_URI -n IMAGE_NAME --env-manager local
| Part | What it means |
|---|---|
-m MODEL_URI | The model to package, such as models:/<model ID> or models:/<name>@<alias> |
-n IMAGE_NAME | The image's name, optionally with a tag, such as iris-species:1; the default is mlflow-pyfunc-servable |
--env-manager local | Install the model's packages into the image's own Python. Leave out uv: the build doesn't support it inside the image |
The CLI reference: "Builds a Docker image whose default entrypoint serves an MLflow model at port 8080, using the python_function flavor." Without --env-manager, the default is virtualenv, which builds on Ubuntu and installs Python with pyenv inside the image.
Prerequisites
- Docker Desktop, installed and running. Docker's licence terms: it's free for "Small businesses (fewer than 250 employees AND less than $10 million in annual revenue)", personal use, education and non-commercial open source projects, and "requires a paid subscription" for professional use in larger organizations and for government entities.
- An MLflow tracking server at
http://127.0.0.1:8080, such asmlflow server --port 8080. mlflowandscikit-learnin your project, for example withuv add mlflow scikit-learn pandas.- About 1.3 GB of free disk space for the image.
Step 1: Log a Model
The example logs a logistic regression trained on the iris flowers:
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("build-docker-example")
iris = load_iris(as_frame=True)
X, y = iris.data, iris.target_names[iris.target] # each flower's species name
model = LogisticRegression(max_iter=1000).fit(X, y)
with mlflow.start_run():
info = mlflow.sklearn.log_model(model, name="model", input_example=X.head(3))
print("Model URI:", info.model_uri)
uv run python log_build_model.py
Model URI: models:/m-3db445b919d042a298b11e079b7f9a25
Step 2: Build the Image
- macOS / Linux
- Windows (PowerShell)
export MLFLOW_TRACKING_URI=http://127.0.0.1:8080
uv run mlflow models build-docker -m "models:/m-3db445b919d042a298b11e079b7f9a25" -n iris-species --env-manager local
$env:MLFLOW_TRACKING_URI = "http://127.0.0.1:8080"
uv run mlflow models build-docker -m "models:/m-3db445b919d042a298b11e079b7f9a25" -n iris-species --env-manager local
2026/11/26 05:11:00 INFO mlflow.pyfunc.backend: Building docker image with name iris-species
[+] Building 199.9s (14/14) FINISHED
=> [1/9] FROM docker.io/library/python:3.15.2-slim@sha256:63bb298d2b7b88f176f51b5189f54ce6535c997682ea70885d2132f5d473a8a5
=> [2/9] RUN printf '%s\n' 'Acquire::http::Timeout "30";' 'Acquire::https::Timeout "30";' 'Acquire::Retries "3";' > /etc/apt/apt.conf.d/99-mlflow-timeouts
=> [3/9] RUN apt-get -y update && apt-get install -y --no-install-recommends nginx
=> [4/9] WORKDIR /opt/mlflow
=> [5/9] RUN pip install mlflow==3.18.0
=> [6/9] COPY model_dir /opt/ml/model
=> [7/9] RUN python -c "from mlflow.models import container as C; C._install_pyfunc_deps('/opt/ml/model', install_mlflow=False, env_manager='local');"
=> [8/9] RUN chmod o+rwX /opt/mlflow/
=> [9/9] RUN rm -rf /var/lib/apt/lists/*
=> exporting to image
=> => naming to docker.io/library/iris-species

MLflow downloaded the model from the tracking server, wrote a Dockerfile next to it, and ran docker build. The nine steps:
FROM python:3.15.2-slim: start from Python 3.15.2, the version the model was logged with.apt-get install ... nginx: nginx, the web server that takes requests on port 8080.pip install mlflow==3.18.0: MLflow itself, the longest step at 135.8 seconds.COPY model_dir /opt/ml/modeland_install_pyfunc_deps(...): the model and its packages, from itsrequirements.txt, in 27.2 seconds.chmodandrm -rf /var/lib/apt/lists/*: open the folder's permissions, and clean up to keep the image smaller.
The whole build took 199.9 seconds. A second image for another model reuses steps 1 to 5 from Docker's cache.
Step 3: Check the Image
docker images iris-species
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
iris-species:latest 7d3dbae6506a 1.24GB 0B
The image takes 1.24 GB on disk, most of it Python and MLflow's packages. Without a tag in -n, Docker named it iris-species:latest.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
--env-manager uv | The build fails at step 7 with Invalid value for env_manager: uv | --env-manager local: Fixed: Invalid value for env_manager: uv |
| Building with Docker Desktop stopped | Cannot connect to the Docker daemon, then RuntimeError: Docker build failed. | Start Docker Desktop first: Fixed: Cannot connect to the Docker daemon |
MLFLOW_TRACKING_URI not set | MLflow looks in a new local database, creates mlflow.db in the folder, and stops: Logged model with ID '...' not found. | Set it in the terminal that builds, and delete the stray mlflow.db |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
docker run warns that the image is linux/amd64 | MLflow builds every image for linux/amd64 | Fixed: The requested image's platform does not match |
Steps say CACHED | Docker reuses steps it has built before | Expected; it makes the next build faster |
FAQ
Does the image need the tracking server to run?
No: the model is copied into the image at step 6. The tracking server is only needed while building.
Which platform is the image for?
linux/amd64, always: MLflow passes --platform linux/amd64 to docker build, so the image runs on most servers. On an Apple silicon Mac it runs under emulation.
Conclusion
mlflow models build-docker -m "models:/m-3db445b9..." -n iris-species --env-manager local built a 1.24 GB image in 200 seconds: Python 3.15.2, nginx, MLflow 3.18.0, the model and its packages, with a web server that answers on port 8080.
Additional Resources
- mlflow models build-docker — the command, its options and the container's settings.
- MLflow Serving — how MLflow packages models in containers.
