This guide explains how to run Ollama locally in Docker with optional NVIDIA GPU acceleration, and how to connect it to the app via .env.
-
Docker + Docker Compose installed
-
If you want GPU support:
- NVIDIA GPU
- NVIDIA drivers installed on the host
- NVIDIA Container Toolkit installed (so Docker can access the GPU)
If you don’t have GPU, you can still run Ollama on CPU - just skip the GPU parts - still not recommended.
Run:
docker run --rm --gpus all nvidia/cuda:12.4.1-base-ubuntu22.04 nvidia-smiIf everything is set up correctly, you should see your GPU listed.
If you get an error like “could not select device driver”, GPU runtime is not configured (you likely need NVIDIA Container Toolkit / drivers). Check NVIDIA docs for details.
If you are having any problems - google it or ask GPT. You can also open an issue in the repo.
Create (or extend) your docker-compose.yml with an ollama service:
services:
ollama:
image: ollama/ollama:latest
restart: unless-stopped
ports:
- "11434:11434"
volumes:
- ollama_data:/root/.ollama
environment:
- OLLAMA_HOST=0.0.0.0:11434
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
interval: 30s
timeout: 10s
retries: 3
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
volumes:
ollama_data:ports: 11434:11434exposes Ollama to the host athttp://localhost:11434ollama_datapersists downloaded models across restarts- The
deploy.resources...GPU section is the part that enables NVIDIA GPUs
3) Start Ollama (if you are using CMake and the same yml file as for the app - make commands will work out of the box)
docker compose up -d ollamaCheck logs:
docker compose logs -f ollamaCheck health endpoint:
curl -s http://localhost:11434/api/tagsIf it returns JSON, Ollama is alive.
Ure the sctipt:
chmod +x scripts/ollama_pull_model.sh
./scripts/ollama_pull_model.sh qwen2.5:7b-instructOR pick a model and pull it with docker compose:
docker compose exec ollama ollama pull qwen2.5:7b-instructList installed models:
docker compose exec ollama ollama listIn your .env:
APP_AI_ENABLED=true
# Local Ollama API
APP_AI_BASE_URL=http://localhost:11434/api/chat
APP_AI_MODEL=qwen2.5:7b-instruct
APP_AI_API_KEY=ollama
# FOR LOCAL OLLAMA SHOULD BE 1 ( ! )
APP_HTTP_CONCURRENCY=1
APP_HTTP_TIMEOUT_S=30.0
APP_AI_TEMPERATURE=0.2
APP_AI_SPAM_THRESHOLD=0.3If your app container is in the same docker-compose.yml network, then localhost inside the app container points to itself, not Ollama.
Use the service name instead:
APP_AI_BASE_URL=http://ollama:11434/api/chatYou can test Ollama directly:
curl http://localhost:11434/api/chat -s -H "Content-Type: application/json" -d '{
"model": "qwen2.5:7b-instruct",
"messages": [
{"role":"system","content":"Return ONLY a number between 0.0 and 1.0."},
{"role":"user","content":"DM me for a quick deal"}
],
"stream": false
}'- Re-run the
nvidia-smiDocker test (Step 1) - Confirm NVIDIA drivers installed on host
- Confirm NVIDIA Container Toolkit installed
- Confirm Docker daemon is configured for NVIDIA runtime
- Lower concurrency:
APP_HTTP_CONCURRENCY=1 - Use a smaller model
- Ensure GPU is actually being used (check container logs and host GPU utilization)