Run Ollama Locally with Docker: Complete 2026 Setup Guide
Run Ollama Locally with Docker: Complete 2026 Setup Guide。Ollama is easy to install and annoying to keep tidy: models scatter across home directories, upgrades break de
📜 Table of Contents
📖 Keep reading
Running Ollama Locally with Docker: Complete 2026 Deployment Guide
Ollama is easy to install and annoying to keep tidy: models scatter across home directories, upgrades break dependencies, and pairing it with a web UI and a workflow tool means three separate installs. Docker fixes most of that. This guide goes from prerequisites to a full Compose stack, then covers model storage, tuning variables, and exposing the API safely.
Why containerize
- Version pinning: tag the image, roll back by changing the tag
- Dependency isolation: CUDA runtime is handled by NVIDIA Container Toolkit
- Easy networking: Open WebUI and n8n share one compose network and address each other by service name
- Clean removal: delete containers and volumes, nothing left behind
Costs: GPU passthrough needs nvidia-container-toolkit, volumes live on the system disk by default, and hardcoded host paths must be updated.
Prerequisites
docker --version
nvidia-smi
docker run --rm --gpus=all nvidia/cuda:12.4.1-base-ubuntu22.04 nvidia-smi
If the third command shows no GPU, the toolkit is missing or Docker was not restarted after install: nvidia-ctk runtime configure --runtime=docker && systemctl restart docker. CPU-only setups skip the GPU flags and run an order of magnitude slower.
Minimal run
docker run -d --name ollama --gpus=all \
-v ollama:/root/.ollama -p 11434:11434 \
--restart unless-stopped ollama/ollama
curl http://localhost:11434/api/tags
docker exec -it ollama ollama pull qwen2.5:7b
Model tags change over time, so check ollama.com/library rather than copying names from old tutorials. For a large disk, replace the named volume with -v /data/ollama:/root/.ollama.
Compose stack
Run Ollama, Open WebUI and n8n together. Key detail: Open WebUI must use OLLAMA_BASE_URL=http://ollama:11434, the service name, not localhost. Map container ports 8080 (Open WebUI) and 5678 (n8n) to whatever host ports you prefer, and give each service a named volume.
Tuning variables
| Variable | Purpose | Typical use |
|---|---|---|
| OLLAMA_HOST | Listen address | Default 0.0.0.0:11434 |
| OLLAMA_MODELS | Model directory | Point at the big disk |
| OLLAMA_KEEP_ALIVE | How long a model stays loaded | 30m for chat, 5m for batch |
| OLLAMA_NUM_PARALLEL | Concurrent requests per model | Raise if VRAM allows |
| OLLAMA_MAX_LOADED_MODELS | Models kept in memory | 1 on small cards |
| OLLAMA_FLASH_ATTENTION | Flash attention | Enable where supported |
| OLLAMA_KV_CACHE_TYPE | KV cache precision | q8_0 saves VRAM |
| OLLAMA_CONTEXT_LENGTH | Default context | Size to the task |
Variable names evolve; verify against the official docs before production.
Storage and migration
Models live in /root/.ollama/models inside the container. Back them up with tar -czf models.tar.gz -C /data ollama/models, or use rsync for incremental sync between machines. ollama ps shows which model is resident, how much memory it uses, and when it will unload.
Exposing the API
Ollama ships with no authentication. Options: restrict the security group to your own IP and tunnel with ssh -L 11434:localhost:11434, put Nginx in front with Basic Auth, or route everything through an API gateway that issues keys. Browser access also needs OLLAMA_ORIGINS for CORS.
FAQ
Q: nvidia-smi not found inside the container? The official image does not ship it. Test with a CUDA base image instead, or confirm GPU use with ollama ps.
Q: Pull hangs? Check df -h first, then outbound network access; corporate proxies need HTTP_PROXY in the compose environment.
Q: First answer slow, later fast? The model has to load into VRAM, and reloads after OLLAMA_KEEP_ALIVE expires. Longer keep-alive trades VRAM for stable latency.
Q: Two models at once? Set OLLAMA_MAX_LOADED_MODELS above 1 if total VRAM fits. Two small models often beat one large model queuing.
Q: Slower than the binary install? Container overhead is negligible; GPU and quantization decide performance, not packaging.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.