Sub topic 16
Deploying with the vLLM container image
This chapter shows how to run vLLM from the official container image. It covers GPU access, model caching, API options, health checks, and common failures. It assumes you know basic Docker commands, have read the chapters on vllm serve and memory tuning, and have an NVIDIA GPU host.
Why use a container
vLLM depends on specific versions of Python, PyTorch, CUDA libraries, and attention kernels. A mismatch between them is a frequent cause of failed installs. The official image vllm/vllm-openai bundles a tested combination. The host then needs only three things:
- An NVIDIA driver that is new enough for the CUDA version in the image.
- Docker.
- The NVIDIA Container Toolkit, which lets containers see the GPUs.
The image's entrypoint starts the same OpenAI-compatible server you ran with vllm serve. Arguments you pass after the image name go to that server.
Running the image
This command is the one from the plan for this chapter:
docker run --runtime nvidia --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
-p 8000:8000 \
--ipc=host \
vllm/vllm-openai:latest \
--model Qwen/Qwen2.5-1.5B-InstructEach part does a specific job:
| Part | Purpose |
|---|---|
| `--runtime nvidia --gpus all` | Gives the container access to every GPU on the host. |
| `-v ~/.cache/huggingface:/root/.cache/huggingface` | Mounts the host's Hugging Face cache so weights persist between runs. |
| `-p 8000:8000` | Publishes the API port. |
| `--ipc=host` | Shares the host's inter-process memory, which PyTorch uses for communication between worker processes. |
| `vllm/vllm-openai:latest` | The image and tag. |
| `--model ...` | The first server argument, which selects the model. |
Why the cache mount matters
Without the volume, the container downloads the weights into its own writable layer. When you remove the container, the download is lost. A 32B model can be tens of gigabytes, so each restart would repeat a long download. With the mount, the first start downloads and later starts load from disk.
Why `--ipc=host` matters
PyTorch uses shared memory to exchange tensors between processes. Docker gives a container a small /dev/shm by default. With tensor parallelism, the workers can fail with bus errors or hangs when that space runs out. --ipc=host removes the limit. A narrower alternative is --shm-size with an explicit value, such as --shm-size=16g.
Passing server settings
Arguments after the image name are the same flags you used with vllm serve. You can reproduce your tuned settings and add the multi-GPU flags:
docker run --runtime nvidia --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
-p 8000:8000 \
--ipc=host \
vllm/vllm-openai:latest \
--model Qwen/Qwen2.5-32B-Instruct \
--tensor-parallel-size 4 \
--max-model-len 8192 \
--gpu-memory-utilization 0.90 \
--enable-prefix-cachingCheck that --tensor-parallel-size does not exceed the number of GPUs the container can see. To expose only some GPUs, replace --gpus all with a device list, for example --gpus '"device=0,1"'.
Private and gated models
Some models require a Hugging Face account and accepted license terms. Pass a token as an environment variable instead of writing it into a file or image:
docker run --runtime nvidia --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=$HF_TOKEN" \
-p 8000:8000 \
--ipc=host \
vllm/vllm-openai:latest \
--model meta-llama/Llama-3.1-8B-InstructThe shell expands $HF_TOKEN from your environment. Do not bake it into a Dockerfile, because image layers keep it.
Checking that the server is ready
Loading weights and profiling memory can take from seconds to several minutes. The port may be open before the model is ready, so test the HTTP endpoints rather than the port:
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8000/health
curl -s http://localhost:8000/v1/models/health returns HTTP 200 when the server can accept requests. /v1/models lists the served model name, which is the value clients must pass as model. The metrics endpoint from the monitoring chapter, /metrics, is served on the same port.
Running as a managed service
A foreground docker run is fine for testing. For a long-lived service you want automatic restarts, a name, and a health check. Docker Compose expresses this in one file:
services:
vllm:
image: vllm/vllm-openai:v0.6.3
command: >
--model Qwen/Qwen2.5-1.5B-Instruct
--max-model-len 4096
--gpu-memory-utilization 0.85
ports:
- "8000:8000"
volumes:
- ~/.cache/huggingface:/root/.cache/huggingface
ipc: host
restart: unless-stopped
environment:
- HF_TOKEN=${HF_TOKEN:-}
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 5s
retries: 3
start_period: 300sReplace the version tag with the release you have tested. The start_period gives the model time to load before failed checks count against the container. Set it to cover your slowest expected load.
Deployment flow
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| `could not select device driver "nvidia"` | NVIDIA Container Toolkit missing or Docker not restarted after install. | Install the toolkit, configure the Docker runtime, restart Docker. |
| CUDA error about driver version | Host driver is older than the image's CUDA build. | Update the driver or choose an older image tag. |
| Out-of-memory at startup | `--gpu-memory-utilization` too high, another process on the GPU, or `--max-model-len` too large for the KV cache. | Lower utilization or context length, or free the GPU. |
| Bus error or hang with several GPUs | Shared memory too small. | Add `--ipc=host` or a larger `--shm-size`. |
| Model downloaded again on every start | No cache volume. | Mount the Hugging Face cache. |
| 401 or 403 while downloading | Gated model and no valid token. | Accept the license on Hugging Face and set `HF_TOKEN`. |
| Connection refused from another machine | Port not published, or a firewall blocks it. | Check `-p` and network rules. |
Security notes
The server has no authentication by default. Publishing -p 8000:8000 exposes it on every network interface of the host. Use one of these measures:
- Bind to localhost with
-p 127.0.0.1:8000:8000and put a reverse proxy with authentication in front. - Pass
--api-keyto the server so it requires a bearer token. Clients then send that value asapi_keyin the OpenAI client. - Restrict access with network rules.
Mounting the cache read-write lets the container write into your home directory. If the weights are already downloaded, you can mount the cache with :ro and set HF_HUB_OFFLINE=1 to prevent downloads.
Exercises
Exercise
A container runs a 32B model with tensor parallelism on four GPUs. It starts, then fails with a bus error during initialization. You did not pass --ipc=host. What is the cause, and what are two ways to fix it?
Show answer
The worker processes exchange data through shared memory, and Docker's default /dev/shm is too small. Fix it by adding --ipc=host, or by setting a larger --shm-size, such as --shm-size=16g.
Exercise
Each time you restart your container, it spends several minutes downloading the model. What change removes this delay, and why does it work?
Show answer
Mount the host's Hugging Face cache into the container with -v ~/.cache/huggingface:/root/.cache/huggingface. The weights are then stored on the host and survive container removal, so later starts load them from disk instead of downloading them.
Exercise
Why is checking that port 8000 is open a poor readiness test, and what should you check instead?
Show answer
The port can accept connections before the model has finished loading and the KV cache is allocated. Request /health, which returns 200 when the server can accept requests, or /v1/models to confirm the model is registered.