Skip to main content

How to Build a Docker Image for an MLflow Model

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
mlflow models build-docker -m MODEL_URI -n IMAGE_NAME --env-manager local
PartWhat it means
-m MODEL_URIThe model to package, such as models:/<model ID> or models:/<name>@<alias>
-n IMAGE_NAMEThe image's name, optionally with a tag, such as iris-species:1; the default is mlflow-pyfunc-servable
--env-manager localInstall 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 as mlflow server --port 8080.
  • mlflow and scikit-learn in your project, for example with uv 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:

log_build_model.py
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)
Terminal
uv run python log_build_model.py
Output (last line)
Model URI: models:/m-3db445b919d042a298b11e079b7f9a25

Step 2: Build the Image​

Terminal
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
Output (excerpt)
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

The build's output in a terminal: Building docker image with name iris-species, the nine steps from FROM python:3.15.2-slim to rm -rf /var/lib/apt/lists, and naming to docker.io/library/iris-species, FINISHED in 199.9 seconds.

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/model and _install_pyfunc_deps(...): the model and its packages, from its requirements.txt, in 27.2 seconds.
  • chmod and rm -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​

Terminal
docker images iris-species
Output
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​

MistakeWhat happensDo this instead
--env-manager uvThe 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 stoppedCannot 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 setMLflow 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​

SymptomLikely causeFix
docker run warns that the image is linux/amd64MLflow builds every image for linux/amd64Fixed: The requested image's platform does not match
Steps say CACHEDDocker reuses steps it has built beforeExpected; 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​