Home / AI & GenAI

AI & GenAICheat sheetIntermediate

Docker GPU containers cheat sheet for LLM serving: NVIDIA Container Toolkit, --gpus flags, CDI and common errors

Commands for running vLLM, Ollama and other model servers in Docker on NVIDIA GPUs - toolkit setup, picking GPUs, CDI, shared memory and fixes for the errors you hit most.

Deepak Bhardwaj6 min read

Applies toNVIDIA Container Toolkit 1.14+, Docker Engine 25+ (CDI), Podman 4+, Linux GPU hosts and VMs (Ubuntu, RHEL/Oracle Linux)

When to use this

You run a model server (vLLM, Ollama, TEI, llama.cpp, Triton) in Docker on a GPU VM, and the container can't see the GPU, sees the wrong one, or loses it after a while. This page covers the host side: driver, NVIDIA Container Toolkit, the Docker flags and the usual errors. For Kubernetes, use the GPU Operator and device plugin instead.

Option names in the toolkit change between releases. Check nvidia-ctk --version and the docs for that version.

Check the host first

nvidia-smi                                  # driver loaded, GPUs listed?
nvidia-ctk --version                        # toolkit installed?
docker info | grep -iE "runtimes|default runtime|cgroup driver"
docker run --rm --gpus all nvidia/cuda:12.4.1-base-ubuntu22.04 nvidia-smi

If nvidia-smi fails on the host, fix the driver first. No container setting can make up for a missing or broken driver.

Install and configure the toolkit

Ubuntu/Debian (after adding NVIDIA's repository as described in the install guide):

sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

RHEL, Oracle Linux and similar:

sudo dnf install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

nvidia-ctk runtime configure adds the nvidia runtime to /etc/docker/daemon.json. Check the result:

cat /etc/docker/daemon.json

For containerd or Podman, use --runtime=containerd or CDI (below).

Choose which GPUs a container gets

Goal Flag
All GPUs --gpus all
Any 2 GPUs --gpus 2
Specific GPUs by index --gpus '"device=0,1"'
Specific GPU by UUID --gpus '"device=GPU-3f2a..."'
Limit driver features --gpus 'all,"capabilities=compute,utility"'

The double quotes inside single quotes are needed when the device list has a comma.

Get stable UUIDs (indexes can change after a reboot or hardware change):

nvidia-smi -L
nvidia-smi --query-gpu=index,uuid,name,memory.total --format=csv

Tip

On multi-GPU hosts running several model servers, pin each container to GPUs by UUID. Two servers that both get --gpus all will fight over memory, and one of them will fail with CUDA out of memory.

Inside the container, CUDA_VISIBLE_DEVICES renumbers what the process sees. A container started with device=2,3 sees them as GPU 0 and 1.

Run common model servers

vLLM with tensor parallelism across two GPUs:

docker run -d --name vllm --gpus '"device=0,1"' --ipc=host \
  -p 127.0.0.1:8000:8000 \
  -v /srv/hf-cache:/root/.cache/huggingface \
  -e HF_TOKEN \
  vllm/vllm-openai:<pinned-tag> \
  --model <org/model> --tensor-parallel-size 2

Ollama on one GPU:

docker run -d --name ollama --gpus '"device=2"' \
  -p 127.0.0.1:11434:11434 -v ollama:/root/.ollama ollama/ollama

Pin image tags. latest can bring in a CUDA version your driver doesn't support.

Shared memory for multi-GPU work

Docker gives containers a 64 MB /dev/shm by default. PyTorch data loaders and NCCL across several GPUs need more. Use one of:

--ipc=host          # share the host IPC namespace (vLLM's docs suggest this)
--shm-size=16g      # or a fixed, larger /dev/shm

--ipc=host weakens isolation between the container and host, so prefer --shm-size on shared hosts.

Use CDI instead of the runtime hook

The Container Device Interface (CDI) describes GPUs in a spec file that Docker 25+, Podman and containerd can read. NVIDIA recommends it on systemd-managed hosts because device nodes survive container updates.

sudo nvidia-ctk cdi generate --output=/etc/cdi/nvidia.yaml
nvidia-ctk cdi list

Expected output lists names such as nvidia.com/gpu=0, nvidia.com/gpu=<uuid> and nvidia.com/gpu=all.

Run with CDI:

# Podman
podman run --rm --device nvidia.com/gpu=all ubuntu nvidia-smi -L

# Docker 25+ (CDI may need enabling: "features": {"cdi": true} in daemon.json on some versions)
docker run --rm --device nvidia.com/gpu=0 ubuntu nvidia-smi -L

Warning

A CDI spec is a snapshot of the driver files. After a driver upgrade, regenerate it, or containers fail to start with missing library or device errors. Newer toolkit releases can refresh the spec automatically; check whether yours does.

Common errors

Error or symptom Cause Fix
could not select device driver "" with capabilities: [[gpu]] Toolkit not installed or Docker not restarted after configuring it Install the toolkit, run nvidia-ctk runtime configure --runtime=docker, restart Docker
nvidia-smi: command not found in container Image has no nvidia-smi and the utility capability wasn't injected Use --gpus all (includes utility) or set NVIDIA_DRIVER_CAPABILITIES=compute,utility
Failed to initialize NVML: Unknown Error after hours or days systemctl daemon-reload on a systemd cgroup host removed GPU access from running containers Restart the container now; move to CDI, or follow the toolkit troubleshooting guide for your version
CUDA driver version is insufficient for CUDA runtime version Image built for a newer CUDA than the host driver supports Use an image tag for your driver's CUDA version, or upgrade the driver
Model server reports 0 GPUs, host shows GPUs Container started without --gpus or --device, or CUDA_VISIBLE_DEVICES set to an empty or wrong value Check docker inspect and the env vars
NCCL errors or bus errors on multi-GPU start /dev/shm too small --ipc=host or --shm-size=16g
CUDA OOM at start, but GPU looked free Another container or host process holds memory on the same GPU nvidia-smi --query-compute-apps=pid,used_memory --format=csv; pin containers to separate GPUs

Check what a running container was given:

docker inspect vllm --format '{{json .HostConfig.DeviceRequests}}' | jq
docker inspect vllm --format '{{json .HostConfig.Devices}}' | jq
docker exec vllm sh -c 'env | grep -E "NVIDIA|CUDA"; nvidia-smi -L'

DeviceRequests is filled in for --gpus. CDI devices show up under Devices or in the CDI-related fields, depending on Docker version.

Compose file

services:
  vllm:
    image: vllm/vllm-openai:<pinned-tag>
    command: ["--model", "<org/model>", "--tensor-parallel-size", "2"]
    ports: ["127.0.0.1:8000:8000"]
    shm_size: "16gb"
    volumes: ["/srv/hf-cache:/root/.cache/huggingface"]
    restart: unless-stopped
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              device_ids: ["0", "1"]
              capabilities: [gpu]

Use count: all instead of device_ids to give the service every GPU. device_ids also accepts UUIDs.

Prevent it next time

  • Pin the driver, toolkit and image versions together, and upgrade them as a set on one host first.
  • Regenerate the CDI spec in the same automation that upgrades the driver.
  • Add a health check that runs nvidia-smi -L (or calls the server's health endpoint) inside each container, and alert when it fails. This catches the NVML "Unknown Error" case.
  • Pin containers to GPUs by UUID and keep a host-level record of which service owns which GPU.
  • Bind model servers to 127.0.0.1 or a private interface and put an authenticating proxy in front.