Developer Tools guide
Ollama Docker Compose Setup: Models, GPUs, and Network Safety
Run Ollama with persistent models, optional NVIDIA or AMD acceleration, verified API access, controlled networking, and reversible upgrades.
Published and reviewed by OpenAlt · September 25, 2026

TL;DR
Run Ollama with Docker Compose, mount /root/.ollama to persistent storage, and bind port 11434 only to loopback unless you deliberately protect it behind an authenticated proxy. Start with the CPU configuration, then add the NVIDIA or AMD override that matches your host.
The official Docker documentation uses the ollama/ollama image, port 11434, and /root/.ollama for model data: Ollama Docker documentation.
After starting the service, pull one model, query /api/tags, and send a test request to /api/generate. A durable volume preserves models across container recreation, while pinned image versions make upgrades and rollback predictable.
TOC
- Choose CPU, NVIDIA, or AMD
- CPU Compose baseline
- NVIDIA GPU override
- AMD GPU override
- Pull a model and verify the API
- Network safety
- Backups and reproducible re-pulls
- Updates and rollback
- Troubleshooting
- FAQ
Choose CPU, NVIDIA, or AMD
CPU mode is the simplest starting point and works well for lightweight models, testing, and hosts without compatible graphics hardware. It also has the fewest moving parts: no device mapping, vendor runtime, or GPU-specific image is required.
Use NVIDIA acceleration when the host has a supported NVIDIA GPU and Docker can access it through the NVIDIA Container Toolkit. The toolkit is required for NVIDIA GPU passthrough: NVIDIA Container Toolkit installation guide.
Use AMD acceleration when the host and ROCm-compatible GPU support the required device mappings. GPU acceleration improves generation speed, but it does not remove the need for adequate system memory, storage, or a model that fits the available hardware.
CPU Compose baseline
Create a Compose file with a named volume. The loopback binding keeps the API local to the Docker host:
services:
ollama:
image: ollama/ollama
container_name: ollama
ports:
- "127.0.0.1:11434:11434"
volumes:
- ollama:/root/.ollama
restart: unless-stopped
volumes:
ollama:
Start it with:
docker compose up -d
The named volume is the important durability setting. Containers can be replaced without deleting downloaded models because the model directory lives outside the container’s writable layer.
For health-minded verification, inspect the service state and then query the API:
docker compose ps
curl http://127.0.0.1:11434/api/tags
The official API documentation covers local endpoints, model listing, generation, and other request formats: Ollama API documentation.
NVIDIA GPU override
Keep the CPU file as the base and add a second Compose file for NVIDIA:
services:
ollama:
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
Install and configure the NVIDIA Container Toolkit on the host before starting this configuration. Then run:
docker compose -f compose.yml -f compose.nvidia.yml up -d
The override changes device access while retaining the image, port, volume, and restart policy from the base file. If the container starts but generation remains CPU-bound, check the host driver, toolkit installation, Docker integration, and container logs before changing the model configuration.
AMD GPU override
For an AMD host, use the ROCm image and expose the required device nodes:
services:
ollama:
image: ollama/ollama:rocm
devices:
- /dev/kfd
- /dev/dri
Start it with:
docker compose -f compose.yml -f compose.amd.yml up -d
The exact result depends on the GPU, kernel, ROCm compatibility, and permissions on the host. Treat the override as a hardware-specific configuration: confirm that the devices exist, then verify the API and generation behavior from inside the running service.


Pull a model and verify the API
Pull one model after the container is running:
docker compose exec ollama ollama pull llama3.2
Run a quick local test:
docker compose exec ollama ollama run llama3.2
You can also test the HTTP API directly:
curl http://127.0.0.1:11434/api/generate \
-d '{"model":"llama3.2","prompt":"Say hello in five words.","stream":false}'
A successful /api/tags response confirms that the API is reachable and the model list can be read. A successful generation response confirms that Ollama can load the selected model. If the first request is slow, allow time for model loading and hardware initialization.
Once verification succeeds, compare your deployment with OpenAlt’s Ollama profile, browse related AI & LLM tools, or find more projects in the self-hosting directory.
Network safety
Do not assume that exposing port 11434 publicly adds authentication. It does not. A public binding can allow unauthorized clients to list models, submit prompts, consume compute, and potentially access sensitive local workflows.
The safest default is:
ports:
- "127.0.0.1:11434:11434"
This permits access from the Docker host but not directly from other machines. If another application runs in the same Compose project, prefer an internal Docker network and call the service by its Compose name instead of publishing the port.
For controlled LAN access, bind to a private host address, restrict traffic with the host firewall, and avoid exposing the service to the internet. If remote access is necessary, place Ollama behind a reverse proxy or gateway that provides authentication, encryption, request limits, and access logging. Keep the Ollama port private behind that layer.
Network isolation is separate from model security. Prompts, generated content, logs, and client credentials still deserve normal secrets-management and access-control practices.
Backups and reproducible re-pulls
The ollama volume contains the downloaded model data. Backing it up preserves the exact local state and avoids downloading models again after disk failure or migration. The tradeoff is storage: model files can be large, and backups may take considerable time and space.
A reproducible re-pull strategy saves less backup space. Recreate the volume, install the same Ollama image version, and pull the required models again. This is simpler, but it depends on download availability, network bandwidth, registry access, and the exact model reference remaining usable.
For an important host, keep a written model inventory and back up the volume or its underlying storage. For an experimental host, re-pulling may be sufficient.
Updates and rollback
Pin the Ollama image to a known release tag in production instead of relying indefinitely on the moving ollama/ollama tag. Record the selected version in version control, update deliberately, and keep the previous image reference available.
Before upgrading, confirm that the model volume is backed up or recoverable. Then recreate the service with the new image and run the same checks: docker compose ps, /api/tags, and a small generation request.
If behavior regresses, stop the service, restore the previous image reference, and start it with the existing volume. Keeping model data separate from the container makes image rollback independent from model re-downloads.
Troubleshooting
| Symptom | Likely check |
|---|---|
| Port connection fails | Confirm the container is running and port 11434 is bound on the expected address. |
| Models disappear | Verify that /root/.ollama is mounted to the intended named volume. |
| API responds but generation fails | Confirm the model was pulled successfully and inspect container logs. |
| NVIDIA acceleration is missing | Check the host driver, NVIDIA Container Toolkit, Docker integration, and GPU reservation. |
| AMD acceleration is missing | Confirm /dev/kfd, /dev/dri, the ROCm image, permissions, and host compatibility. |
| Remote client cannot connect | Check loopback binding, firewall rules, private routing, and proxy configuration. |
FAQ
Does Ollama need a GPU?
No. Ollama can run in CPU mode. A compatible NVIDIA or AMD GPU can improve performance, but CPU mode is the simplest baseline for testing and lightweight workloads.
Where are Ollama models stored?
With this Compose setup, models are stored in the persistent ollama volume mounted at /root/.ollama inside the container.
Which port does the Ollama API use?
The local API uses port 11434. Binding 127.0.0.1:11434:11434 keeps direct access on the Docker host.
Is the API safe to expose publicly?
Not by default. Public exposure does not provide authentication. Use loopback or private networking, or place the API behind an authenticated and encrypted proxy.
How do upgrades avoid re-downloading models?
Keep models in the persistent volume and change only the Ollama image version. The container can be replaced while the mounted model data remains available.