Skip to main content

How to Generate a Dockerfile for an MLflow Model

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

mlflow models build-docker writes a Dockerfile and builds it in one step, so you never see the file. mlflow models generate-dockerfile stops after writing it: you get a folder with the Dockerfile and the model, to read, change or build yourself. This guide generates one for a wine classifier, goes through it, and builds it with docker build, with real output from MLflow 3.18.0.

Syntax​

Syntax
mlflow models generate-dockerfile -m MODEL_URI -d FOLDER --env-manager local
PartWhat it means
-m MODEL_URIThe model to package
-d FOLDERWhere to write the Dockerfile and the model; the default is mlflow-dockerfile
--env-manager localInstall the model's packages into the image's own Python, as build-docker --env-manager local does

The CLI reference: "Generates a directory with Dockerfile whose default entrypoint serves an MLflow model at port 8080 using the python_function flavor. ... This Dockerfile defines an image that is equivalent to the one produced by mlflow models build-docker."

Prerequisites​

  • 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.
  • Docker Desktop, running, only for Step 4.

Step 1: Log a Model​

The example logs a wine classifier, which tells three cultivars apart from their chemistry:

log_dockerfile_model.py
import mlflow
from sklearn.datasets import load_wine
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("generate-dockerfile-example")

X, y = load_wine(return_X_y=True, as_frame=True)
model = make_pipeline(StandardScaler(), LogisticRegression()).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_dockerfile_model.py
Output (last line)
Model URI: models:/m-80064c54349d4217bd8bc566216e411b

Step 2: Generate the Dockerfile​

Terminal
export MLFLOW_TRACKING_URI=http://127.0.0.1:8080
uv run mlflow models generate-dockerfile -m "models:/m-80064c54349d4217bd8bc566216e411b" -d wine-dockerfile --env-manager local
Output
2026/11/26 05:17:10 INFO mlflow.models.cli: Generating Dockerfile for model models:/m-80064c54349d4217bd8bc566216e411b
2026/11/26 05:17:10 INFO mlflow.models.flavor_backend_registry: Selected backend for flavor 'python_function'
2026/11/26 05:17:11 INFO mlflow.models.cli: Generated Dockerfile in directory wine-dockerfile

The folder it wrote:

wine-dockerfile
wine-dockerfile
wine-dockerfile/model_dir
wine-dockerfile/model_dir/python_env.yaml
wine-dockerfile/model_dir/requirements.txt
wine-dockerfile/model_dir/MLmodel
wine-dockerfile/model_dir/serving_input_example.json
wine-dockerfile/model_dir/input_example.json
wine-dockerfile/model_dir/conda.yaml
wine-dockerfile/model_dir/model.skops
wine-dockerfile/Dockerfile

The generate-dockerfile command's output in a terminal, then the wine-dockerfile folder: the Dockerfile and model_dir with MLmodel, model.skops, requirements.txt, python_env.yaml and the input examples.

model_dir is the model as MLflow logged it: MLmodel, the saved model, model.skops, and its packages in requirements.txt.

Step 3: Read the Dockerfile​

wine-dockerfile/Dockerfile
# Build an image that can serve mlflow models.
FROM python:3.15.2-slim

# Fail fast on a stalled package mirror
RUN printf '%s\n' \
'Acquire::http::Timeout "30";' \
'Acquire::https::Timeout "30";' \
'Acquire::Retries "3";' \
> /etc/apt/apt.conf.d/99-mlflow-timeouts
RUN apt-get -y update && apt-get install -y --no-install-recommends nginx



WORKDIR /opt/mlflow

# Install MLflow
RUN pip install mlflow==3.18.0

# Copy model to image and install dependencies
COPY model_dir /opt/ml/model
RUN python -c "from mlflow.models import container as C; C._install_pyfunc_deps('/opt/ml/model', install_mlflow=False, env_manager='local');"

ENV MLFLOW_DISABLE_ENV_CREATION=True

# granting read/write access and conditional execution authority to all child directories
# and files to allow for deployment to AWS Sagemaker Serverless Endpoints
# (see https://docs.aws.amazon.com/sagemaker/latest/dg/serverless-endpoints.html)
RUN chmod o+rwX /opt/mlflow/

# clean up apt cache to reduce image size
RUN rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["python", "-c", "from mlflow.models import container as C; C._serve('local')"]

The generated Dockerfile in a macOS-style code window: FROM python:3.15.2-slim, apt timeouts, nginx, WORKDIR /opt/mlflow, pip install mlflow==3.18.0, COPY model_dir, installing the model's packages, ENV MLFLOW_DISABLE_ENV_CREATION, chmod, apt cleanup, and the ENTRYPOINT that serves the model.

  • FROM python:3.15.2-slim: a small Debian image with Python 3.15.2, the model's version.
  • The two apt lines: timeouts for the package download, then nginx, which listens on port 8080.
  • pip install mlflow==3.18.0: the MLflow version that generated the file.
  • COPY model_dir /opt/ml/model and the python -c line after it: the model, and the packages in its requirements.txt.
  • MLFLOW_DISABLE_ENV_CREATION=True: the packages are already installed, so the container doesn't build an environment when it starts.
  • ENTRYPOINT: what the container runs: MLflow's code that starts nginx and the model server.

There's no USER line, so the container runs as root. docker run --rm --platform linux/amd64 --entrypoint whoami wine-classifier prints root for the image built in Step 4. For a server others can reach, that's worth knowing: anything that breaks into the model server has the container's full rights.

Step 4: Build It Yourself​

Terminal
docker build --platform linux/amd64 -t wine-classifier wine-dockerfile
Output (excerpt)
[+] Building 31.0s (14/14) FINISHED
=> [1/9] FROM docker.io/library/python:3.15.2-slim@sha256:63bb298d2b7b88f176f51b5189f54ce6535c997682ea70885d2132f5d473a8a5
=> CACHED [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
=> CACHED [3/9] RUN apt-get -y update && apt-get install -y --no-install-recommends nginx
=> CACHED [4/9] WORKDIR /opt/mlflow
=> CACHED [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/*
=> => naming to docker.io/library/wine-classifier

--platform linux/amd64 builds for the same platform as build-docker, which always passes it. Steps 2 to 5 say CACHED: Docker reused them from an earlier MLflow image with the same lines, so only the model's own steps ran, and the build took 31 seconds. The image serves the model the same way: How to Run an MLflow Model in a Docker Container.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out --env-managerThe reference says the default picks local when the model records its Python version, and virtualenv otherwisePass it, to be sure which you get

Troubleshooting​

SymptomLikely causeFix
Cannot connect to the Docker daemon during docker buildDocker Desktop isn't runningFixed: Cannot connect to the Docker daemon
The generated file starts FROM ubuntu:24.04--env-manager virtualenv or conda was passedUse --env-manager local for the Python image

FAQ​

Can I change the generated Dockerfile?​

Yes: it's an ordinary Dockerfile, and docker build builds whatever is in it. generate-dockerfile writes the folder's Dockerfile each time it runs, so keep your changed version somewhere else.

Is it the same as build-docker's image?​

With the same --env-manager, yes: the reference calls it "equivalent to the one produced by mlflow models build-docker".

Conclusion​

mlflow models generate-dockerfile -m "models:/m-80064c54..." -d wine-dockerfile --env-manager local wrote a Dockerfile and the model's files, which showed every step build-docker takes: a Python 3.15.2 base, nginx, MLflow 3.18.0, the model and its packages, an entrypoint that serves it, and no USER, so it runs as root. docker build --platform linux/amd64 built it in 31 seconds.

Additional Resources​