Containers are great at packaging an LLM app. They do nothing to hide the cost of a 14 GB model baked into the image. Most of the slow cold starts I've chased on inference services came from that one packaging decision, not from Docker itself.
Key points
- Docker fixes environment drift. It doesn't touch model latency.
- Baking weights into the image makes every new node download gigabytes before it can serve.
- Ship a small app image and mount the weights as a read-only volume.
What Docker actually does for an LLM app
Docker packages your code with the OS libraries and dependencies it needs, so it runs the same in any environment (IBM). Containers share the host's kernel instead of booting a full OS. That's why they're measured in megabytes and start fast (IBM).
For a RAG service, that's exactly what you want. RAG (retrieval-augmented generation) means the service looks up relevant documents and sends them to the model along with the question. You get pinned Python, pinned libraries and the same behaviour in staging and production.
What you don't get is faster tokens. Inference latency comes from the model, the GPU and the prompt length. A container is an isolated process on the host, built on Linux cgroups and namespaces (IBM). It adds no meaningful overhead to generation.
Why baking model weights into a Docker image hurts
An image is a stack of read-only layers. Each layer is the set of filesystem changes made by one build step (Microsoft Learn). Put COPY ./weights /models in your Dockerfile and that layer is your model. A 7B-parameter model stored at 16-bit precision is about 14 GB of weights alone.
Three things follow:
- Slow cold starts. A fresh node has to pull that layer before the container can start, so autoscaling waits on the network.
- Coupled releases. A one-line bug fix and a model upgrade become the same artifact. Rollbacks get heavy.
- Registry bloat. Every model version becomes another multi-gigabyte image.
Layer caching softens this on warm nodes, because Docker only ships the changes between image versions (IBM). But the nodes autoscaling adds under load are exactly the ones with an empty cache. When I've profiled slow scale-ups, pulling the image was usually the biggest part of startup, ahead of loading the model into GPU memory.
Console and cartridge
Baking weights into the image is like soldering the game into the console. Want a new game? Ship a new console.
Mounting the weights keeps the console the same. You swap the cartridge.
How to structure a Docker image for LLM inference
My default split:
- Image: runtime, dependencies and app code. Use a slim base and a multi-stage build. CUDA base images aren't small, but they're shared and cached across services.
- Weights: versioned in object storage, synced to a node-local disk and mounted into the container read-only.
- Config: the model path comes from an environment variable, so one image can run any model version.
The app should fail loudly if the mount is missing, rather than hang. In Python, for example:
import os
from pathlib import Path
# Weights live on a read-only volume, not inside the image
MODEL_DIR = Path(os.environ.get("MODEL_DIR", "/models/current"))
def resolve_model_dir() -> Path:
if not (MODEL_DIR / "config.json").is_file():
raise RuntimeError(f"No model at {MODEL_DIR}. Is the volume mounted?")
return MODEL_DIR
You'd run it with something like docker run --gpus all -v /mnt/models/llm-v3:/models/current:ro -e MODEL_DIR=/models/current my-llm-api:1.4.2.
Where containers still don't help
Mounting doesn't make loading free. The weights still have to move from disk into GPU memory. What you remove is the pull and the coupling. And a slow network file system can wipe out the gain, which is why I prefer a node-local cache over reading weights straight off shared storage.
If your RAG service calls a hosted model API, it has no weights at all. If that image is still several gigabytes, something crept in.
Baking weights in is fine in two cases: small models such as a few-hundred-megabyte embedding model, and air-gapped or edge deployments where a single self-contained artifact is the requirement.
What to do
- Run
docker historyon your inference image and find the biggest layer. - Move the weights to versioned storage, mount them read-only and pass the path through an environment variable.
- Measure cold start in three parts (image pull, weight load, first token) and fix the biggest one first.
