What does `mlflow models build-docker --model-uri` put into the image, and how does the container it builds serve requests?
answer
- dependencies installed at build time
- the model under /opt/ml/model
- a proxy in front on 8080
- only two routes are forwarded
- one worker per CPU
basics
~20 smlflow models build-docker --model-uri copies the model into /opt/ml/model and installs its dependencies while the image builds; the container then runs nginx on port 8080 in front of uvicorn workers, one per CPU unless MLFLOW_MODELS_WORKERS is set.
solid answer
~40 sWith `--model-uri`, `build-docker` downloads the model into the build context, copies it to `/opt/ml/model` and installs its dependencies in a build step, so the container starts without installing anything. Without `--model-uri` you get a generic image that expects a model mounted at `/opt/ml/model` and installs its dependencies every time it starts. The entrypoint starts nginx on 8080 with uvicorn behind it, running `MLFLOW_MODELS_WORKERS` workers or one per CPU. nginx forwards only `/ping` and `/invocations`, so `/health` and `/version` return 404 through port 8080 and a probe should use `/ping`. Java is installed automatically for `spark`, `h2o` and `johnsnowlabs` models; `--install-java` covers other models that need a JVM. `generate-dockerfile` writes the equivalent Dockerfile without building it.
code
bash · 5 linesmlflow models build-docker -m models:/property-value/3 -n property-value:3
docker run --rm -p 5001:8080 -e MLFLOW_MODELS_WORKERS=2 property-value:3
curl -s localhost:5001/ping
curl -s localhost:5001/invocations -H 'Content-Type: application/json' \
-d '{"dataframe_split": {"columns": ["living_area_m2", "bedrooms"], "data": [[84.0, 2]]}}'go deeper
Recall that build-docker serves on port 8080, that the model lives at /opt/ml/model, and that /ping is the health route to probe.
Explain the difference between images built with and without --model-uri, how the worker count is chosen, and when Java is installed.
Catch the production traps: nginx answering /health with 404, the 100 MB body cap, workers following the CPU count, and dependency installs at container start for generic images.
Decide between per-version images and one generic image across many models, weighing build time, registry storage, start-up predictability and what the security scan can see.
## Two kinds of image `mlflow models build-docker` builds an image whose entrypoint serves an MLflow model through the **`python_function`** flavor. What goes into the image depends on whether you pass a model: | | With `--model-uri` | Without `--model-uri` | |---|---|---| | Model files | copied to `/opt/ml/model` at build | mounted at `/opt/ml/model` at run time | | Dependencies | installed in a build step | installed every time the container starts | | Start-up | fast and repeatable | slow, and needs package-index access at boot | | Use | one image per model version | one generic image for many models | For the property-valuation estimator moving off its VM, the per-version image is the usual choice: the dependency install happens once, in CI, and a restart installs nothing. The CLI names the image `mlflow-pyfunc-servable` unless `--name` / `-n` says otherwise. ## What the build does With a model URI the build: 1. downloads the model into the Docker build context; 2. picks a base image — `ubuntu:24.04`, or `python:<version>-slim` when the model records its Python version, needs no Java and the environment manager allows it (with the CLI's default `virtualenv`, the Ubuntu image is used); 3. copies the model to `/opt/ml/model` and runs MLflow's dependency installer for the chosen `--env-manager`; 4. adds system packages a flavor is known to need (for example `libgomp1` for LightGBM); 5. sets `MLFLOW_DISABLE_ENV_CREATION` so the container does not build an environment again at start. **Java** is left out unless needed, as it has been since MLflow 2.10.1. The `spark`, `h2o` and `johnsnowlabs` flavors switch it on automatically; a custom pyfunc model that calls Spark ML needs **`--install-java`**. Java requires the Ubuntu base image. ## How the container serves By default the entrypoint starts two processes and exits when either one dies: - **nginx** listening on **8080**, the port the docs map with `docker run -p 5001:8080`; - **uvicorn** running the scoring server behind it, with **`MLFLOW_MODELS_WORKERS`** workers, defaulting to the container's **CPU count** (not 1, as with `mlflow models serve`). The bundled nginx configuration matters in production: - it forwards only paths matching **`/ping`** and **`/invocations`**; every other path, including **`/health`** and **`/version`**, gets a 404 from nginx; - it caps request bodies on those routes at **100 MB**, so a very large batch body is rejected by nginx before it reaches the model. So a readiness probe pointed at `/health` fails on this image even though the model loaded; point it at `/ping`. Setting **`DISABLE_NGINX=true`** starts the container without nginx; the CLI help suggests it for platforms where nginx is not needed. ## The commands around it - **`mlflow models generate-dockerfile`** writes the same Dockerfile to a directory (`mlflow-dockerfile` by default) without building it — useful when CI must build, scan and sign images itself. - **`--env-manager`** chooses how dependencies are installed during the build. - **`--install-mlflow`** installs the CLI's MLflow version into the model environment. - **`mlflow.models.build_docker`** is the Python form of the same build. Running the image on Kubernetes as an inference service, or pushing it to a managed endpoint, is the job of other tools; this image is MLflow's own server in a box. ## Mistakes seen with these images - Probing `/health` or `/version` through port 8080 and reading the 404 as a dead model. - Leaving the worker count at the CPU default on a large node, so each worker loads its own copy of the model and memory runs out. - Shipping a generic image to a network without a package index, so the first start cannot install the mounted model's dependencies. - Expecting `--install-java` to be needed for a Spark-flavored model; MLflow already turns it on for that flavor. ## Checking the image before it ships 1. `docker run -p 5001:8080 <image>` and `curl localhost:5001/ping` — the model loaded. 2. Post a known listing to `/invocations` and compare the prediction with the training-time value. 3. Confirm the worker count matches the CPU limit you will give the container, since the default follows the CPUs the container sees.
- A readiness probe on `/health` keeps failing for a `build-docker` image although the logs show the model loaded. Why?The image puts nginx on port 8080 in front of uvicorn, and its configuration forwards only `/ping` and `/invocations`. Every other path, `/health` included, gets a 404 from nginx before it reaches the scoring server. Point the probe at `/ping`, which runs the same health handler, or start the container with `DISABLE_NGINX=true` behind a proxy you control.
- Why might a generic image built without `--model-uri` start slowly and fail in a locked-down network?Without a model URI the build has no requirements to install, so the container installs the mounted model's dependencies at every start. That needs a package index at boot, adds minutes to each start and can resolve different versions over time. Building per model version with `--model-uri` moves the install into the image build.
- Every route of the scoring server is reachable on the container's port 8080
- The container runs one uvicorn worker unless told otherwise
- Without --model-uri, dependencies are still installed at build time
- Java is installed into every image built by build-docker
- generate-dockerfile produces a different image from build-docker