Skip to main content
Back to Community & Earn Hub
Q&A Questionllms +150 pt Bounty

Ollama + Docker Compose GPU Passthrough in Linux/WSL2: How to fix 'could not select device driver nvidia' and share VRAM cleanly across concurrent agent workers?

@nikhil_gupta4 days ago 1534 views

We package our local agent stack (FastAPI + Ollama + Redis) using Docker Compose for developers running RTX 4090 / 3090 GPUs on Ubuntu and Windows WSL2.

Two Blocking Issues

  1. On fresh Linux/WSL2 hosts, docker compose up crashes immediately with:
    text
    Error response from daemon: could not select device driver "" with capabilities: [[gpu]]
  2. Once GPU passthrough is enabled, when 4 sub-agents hit Ollama concurrently (qwen2.5-coder:14b + nomic-embed-text), Ollama repeatedly evicts the 14B model to load the embedding model, dropping throughput to a crawl and spiking VRAM past 24GB!

What is the exact nvidia-ctk setup + docker-compose.yml environment configuration (OLLAMA_NUM_PARALLEL, OLLAMA_KV_CACHE_TYPE) to keep both models resident in VRAM simultaneously?

1 Answers

Community Answers

@siddharth_rao Accepted Answer4 days ago

Both issues have clean, deterministic fixes:


1. Fix could not select device driver "" with capabilities: [[gpu]]

Install NVIDIA Container Toolkit, register the Docker runtime via nvidia-ctk, and restart the Docker daemon:

bash
# Ubuntu / WSL2 (Docker Engine)
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

(If using Docker Desktop on Windows WSL2, ensure Settings -> Resources -> WSL Integration is enabled for your distro—Docker Desktop handles GPU passthrough automatically without running systemctl inside WSL2.)


2. Prevent Model Eviction Thrash & Cut KV-Cache VRAM by 50%

By default, OLLAMA_MAX_LOADED_MODELS is conservative and FP16 KV caches consume huge VRAM under OLLAMA_NUM_PARALLEL=4. Set these four environment flags in docker-compose.yml:

yaml
services:
  ollama:
    image: ollama/ollama:latest
    ports:
      - "11434:11434"
    environment:
      - OLLAMA_NUM_PARALLEL=4          # Handle 4 concurrent sub-agent streams
      - OLLAMA_MAX_LOADED_MODELS=2     # Keep 14B LLM + embed model resident in VRAM!
      - OLLAMA_FLASH_ATTENTION=1       # Required for quantized KV cache
      - OLLAMA_KV_CACHE_TYPE=q8_0      # Cuts KV-cache VRAM ~50% with negligible quality loss
      - OLLAMA_KEEP_ALIVE=30m          # Prevent idle eviction between agent turns
    volumes:
      - ollama_data:/root/.ollama
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]

volumes:
  ollama_data:

With OLLAMA_MAX_LOADED_MODELS=2 + OLLAMA_FLASH_ATTENTION=1 + OLLAMA_KV_CACHE_TYPE=q8_0, both qwen2.5-coder:14b (~9GB) and nomic-embed-text (~0.3GB) stay permanently pinned in a 24GB RTX 4090 with 4 parallel slots and zero reload thrash.

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.