refusal rate
Subscribe

Run Ollama in Docker with copy-ready run and compose files for CPU, NVIDIA and AMD ROCm, a persistent model volume, env vars, and why Macs get no GPU in Docker.

[ fig. 01 — how-to ]48×5
[ fig. 01 — how-to ]16×5

Short answer

To run Ollama in Docker, start the official ollama/ollama image with a named volume on /root/.ollama and port 11434. For an NVIDIA GPU, install the NVIDIA Container Toolkit and add --gpus=all (or a deploy.resources device reservation in Compose). For AMD, use the ollama/ollama:rocm tag and pass /dev/kfd and /dev/dri. Docker on macOS has no GPU access, so Mac users should run Ollama natively.

Image
ollama/ollama (CPU, NVIDIA, Vulkan); ollama/ollama:rocm (AMD)
Model path in container
/root/.ollama
Port
11434
GPU in Docker on macOS
Not available (no passthrough)

Key takeaways

  1. The CPU one-liner is docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama. The volume is what keeps your models across container rebuilds.
  2. NVIDIA needs the host driver plus the NVIDIA Container Toolkit. If docker run --rm --gpus all ubuntu nvidia-smi fails, Ollama will not see the GPU either.
  3. AMD needs the :rocm tag and the /dev/kfd and /dev/dri devices. The default image also bundles Vulkan, which uses the same device mounts.
  4. Docker Desktop on macOS has no GPU passthrough, so a containerized Ollama on a Mac runs on CPU. Native Ollama uses Metal.
  5. The image sets OLLAMA_HOST=0.0.0.0:11434, so a plain -p 11434:11434 exposes the API on every host interface. Bind to 127.0.0.1 unless you mean to share it.

Ollama in Docker comes down to three choices: which image tag, which devices the container gets, and where the models live. Get those right and the same ollama/ollama container runs on a laptop CPU, an NVIDIA workstation or an AMD box with one changed line. Everything below comes from Ollama’s own Docker docs and Dockerfile, NVIDIA’s and Docker’s docs, and GitHub reports from people running it.

[ fig. 01 — Which Ollama Docker setup fits your host. Source: docs.ollama.com/docker and docs.ollama.com/faq ]
Decision tree: on Linux or WSL2 with NVIDIA use ollama/ollama with --gpus=all after installing the NVIDIA Container Toolkit; with AMD use ollama/ollama:rocm plus /dev/kfd and /dev/dri; other GPUs can try Vulkan in the default image with /dev/dri; on macOS Docker runs CPU-only, so run Ollama natively for Metal.

How do you run Ollama in Docker?

The CPU-only command from Ollama’s Docker docs is one line:

shell
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama

Three parts matter:

  • -v ollama:/root/.ollama creates a named volume. Models, keys and the manifest live there, so you can delete and recreate the container without downloading 20 GB again.
  • -p 11434:11434 publishes the API. The Dockerfile sets OLLAMA_HOST=0.0.0.0:11434 inside the image, and Docker publishes to all host addresses unless you name one. For a dev box, use -p 127.0.0.1:11434:11434 instead.
  • The image’s entrypoint is /bin/ollama serve, so the container is the server. You talk to it with docker exec or over HTTP.

The same thing as a Compose file, which is what most people keep in a repo:

yaml
services:
  ollama:
    image: ollama/ollama
    container_name: ollama
    ports:
      - "127.0.0.1:11434:11434"
    volumes:
      - ollama:/root/.ollama
    restart: unless-stopped

volumes:
  ollama:

Running on CPU is fine for small models and for CI. One container-specific catch: a GitHub report found that Ollama sizes its thread pool from the host’s core count and ignores a Compose cpus: limit. On an 8-vCPU host with the container capped at 4 CPUs, llama3.2:3b ran at 0.27 tok/s with the default 8 threads and 12.07 tok/s with num_thread set to 4 (issue #17916, community report on Ollama 0.32.15). If you cap CPUs, set num_thread to match in a Modelfile or in the request options.

How do you give the Ollama container an NVIDIA GPU?

Install the NVIDIA driver on the host, install the NVIDIA Container Toolkit, register it with Docker, then add --gpus=all. The driver stays on the host. The container only gets access to it.

On Debian or Ubuntu, the steps from Ollama’s docs and the NVIDIA install guide are:

shell
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey \
  | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -fsSL https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \
  | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \
  | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

On Fedora or RHEL, add the .repo file from the same NVIDIA URL and run sudo dnf install -y nvidia-container-toolkit. Before you touch Ollama, run NVIDIA’s sample workload:

shell
sudo docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi

If that prints your card, start Ollama with the GPU:

shell
docker run -d --gpus=all -v ollama:/root/.ollama -p 127.0.0.1:11434:11434 --name ollama ollama/ollama

Ollama Docker Compose with an NVIDIA GPU

In Compose, the documented way is a device reservation under deploy.resources (Docker Compose GPU docs):

yaml
services:
  ollama:
    image: ollama/ollama
    container_name: ollama
    ports:
      - "127.0.0.1:11434:11434"
    volumes:
      - ollama:/root/.ollama
    restart: unless-stopped
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]

volumes:
  ollama:

Rules from Docker’s docs: capabilities is required, and count and device_ids are mutually exclusive. To pin Ollama to specific cards, replace count: all with device_ids: ['0'] using the IDs from nvidia-smi. Recent Compose versions also accept the shorter gpus: all service attribute (Compose file reference). Older tutorials use runtime: nvidia, which only works when that runtime is registered in Docker’s daemon.json; the device reservation is the form Docker documents.

On NVIDIA Jetson boards, the docs say Ollama can’t detect the JetPack version, so pass -e JETSON_JETPACK=5 or 6. Docker Desktop on Windows works the same way through WSL2, per the Ollama FAQ.

How do you use an AMD GPU with Ollama in Docker?

Use the ollama/ollama:rocm tag and pass the two AMD device nodes, /dev/kfd (the compute interface) and /dev/dri (the GPU render nodes). The run command from the docs:

shell
docker run -d --device /dev/kfd --device /dev/dri -v ollama:/root/.ollama -p 127.0.0.1:11434:11434 --name ollama ollama/ollama:rocm

As Compose:

yaml
services:
  ollama:
    image: ollama/ollama:rocm
    container_name: ollama
    devices:
      - /dev/kfd:/dev/kfd
      - /dev/dri:/dev/dri
    ports:
      - "127.0.0.1:11434:11434"
    volumes:
      - ollama:/root/.ollama
    restart: unless-stopped
    # environment:
    #   - HSA_OVERRIDE_GFX_VERSION=10.3.0   # only for cards ROCm doesn't list

volumes:
  ollama:

The host needs the ROCm v7 driver (GPU docs); the Dockerfile currently builds against ROCm 7.2.1. For a card that ROCm doesn’t officially target, the docs suggest HSA_OVERRIDE_GFX_VERSION set to the nearest supported target (their example: an RX 5400 runs as 10.3.0). On SELinux distros, the docs say containers may be blocked from the devices until you run sudo setsebool container_use_devices=1 on the host. Card support lists and ROCm-versus-Vulkan trade-offs are in our local LLM on AMD GPU guide.

The default ollama/ollama image also bundles Vulkan, enabled when the container can see GPU devices. That gives Intel and older AMD GPUs a path: pass /dev/dri (and /dev/kfd for AMD) to the default image. Set OLLAMA_VULKAN=0 to turn it off or GGML_VK_VISIBLE_DEVICES to pick devices.

Can you use the GPU with Ollama in Docker on a Mac?

No. The Ollama FAQ says GPU acceleration “is not available for Docker Desktop in macOS due to the lack of GPU passthrough and emulation.” Docker on a Mac runs Linux containers in a VM, and the VM sees no Apple GPU, so Ollama inside it falls back to CPU. That applies to Ollama Docker on Apple Silicon too: the arm64 image runs, but only on the CPU cores.

What to do instead:

  1. Run Ollama natively on macOS, where it uses Metal.
  2. Keep the rest of the stack in Docker and point it at the host: http://host.docker.internal:11434. This is the usual layout for Open WebUI with Ollama on a Mac.
  3. If the model must run in a container, Podman’s libkrun provider exposes a virtualized Vulkan GPU. It works but is slower than native, as the numbers below show.
[ fig. 02 — Token generation (tg128) on an M2 Max, llama 7B Q4_0. Community benchmark, llama.cpp discussion #12985 ]
Bar chart of llama.cpp generation speed on an M2 Max with a 7B Q4_0 model: 71.4 tokens per second native with Metal, 35.9 in a Podman container with a virtualized Vulkan GPU, and 28.6 in a CPU-only container.

Those figures come from a community benchmark of llama.cpp, the engine under Ollama, not of Ollama itself (llama.cpp discussion #12985). The gap is still the useful part: a container on a Mac gave up about half the generation speed even with the virtual GPU.

Which environment variables and volumes should you set?

Set the model volume always, and a few server variables when the defaults don’t fit. Put them under environment: in Compose so they live with the service. The defaults below are from the Ollama FAQ and the Dockerfile.

Variable or mountDefaultSet it when
ollama:/root/.ollama (volume)none: models die with the containerAlways. Use a bind mount such as ./models:/root/.ollama to keep models on a chosen disk
OLLAMA_HOST0.0.0.0:11434 in the imageRarely. Control exposure with the -p binding instead
OLLAMA_KEEP_ALIVE5mYou want a model kept in VRAM between requests (-1) or unloaded at once (0); see Ollama keep alive
OLLAMA_CONTEXT_LENGTH4096 per the FAQ; larger on 24 GB+ GPUs per the context docsCoding agents and RAG need longer context
OLLAMA_NUM_PARALLEL1Several users or agents share the server; memory scales with parallel × context
OLLAMA_MAX_LOADED_MODELS3 × GPUs, or 3 on CPUYou swap between models and have the memory
OLLAMA_FLASH_ATTENTION / OLLAMA_KV_CACHE_TYPEauto / f16You want a q8_0 KV cache to fit more context (needs flash attention)
CUDA_VISIBLE_DEVICES / ROCR_VISIBLE_DEVICESallLimit Ollama to some GPUs (UUIDs are more stable than indexes)
HTTPS_PROXYunsetPulls must go through a corporate proxy (the FAQ advises against HTTP_PROXY)
OLLAMA_NO_CLOUDunsetSet to 1 for local-only mode with cloud models disabled

Two notes on volumes. Named volumes land under Docker’s data root, which can fill a small system disk fast. And docker compose down -v deletes the volume with the models in it, so drop the -v unless you mean it. Choosing and moving model directories outside Docker is covered on its own page about Ollama’s model location.

How do you pull and preload models in the Ollama container?

Use the CLI inside the container, or the HTTP API from outside it. Both write to the mounted volume.

shell
docker exec -it ollama ollama pull qwen2.5-coder:7b
docker exec -it ollama ollama run qwen2.5-coder:7b
docker exec ollama ollama list

From the host, curl http://localhost:11434/api/pull -d '{"model":"qwen2.5-coder:7b"}' does the same thing.

There is no built-in “pull on start” flag. A request for one, issue #3369, has been open since March 2024. The workarounds posted there come in three patterns:

  1. Bake models into an image. In a Dockerfile, start the server in the background, pull, and stop it: RUN ollama serve & sleep 5 && ollama pull qwen2.5-coder:7b. This makes images large, but they start ready.
  2. Wrap the entrypoint. A shell script starts ollama serve &, polls until it answers, pulls the list of models, then waits on the server. Kubernetes users in the thread do this in the container command.
  3. Use a one-shot sidecar. A second service that depends_on Ollama runs ollama pull against OLLAMA_HOST=ollama:11434 and exits.

For a team server, pattern 3 keeps the official image untouched, so upgrades stay a docker compose pull. The wider setup (auth, queueing, which runtime) is in self-hosting an LLM for a dev team.

What do developers report in practice?

The recurring problems are about health checks, GPU visibility and Macs, not about Ollama itself.

  • Health checks fail because the image has no curl. A GitHub report found that a Compose healthcheck running curl -f http://localhost:11434/api/tags marked a working container unhealthy, which broke Traefik routing, because curl isn’t in the image (issue #9781, closed as a duplicate of #5389). The current Dockerfile still installs only ca-certificates, libvulkan1 and libopenblas0. One public Compose stack switched its check to the ollama binary, which is always present (growlf/ai-stack PR #17):

    healthcheck:
      test: ["CMD", "ollama", "list"]
      interval: 30s
      timeout: 10s
      retries: 5
  • nvidia-smi working is not proof Ollama uses the GPU. In a Reddit thread, a user got an RTX 3060 working in Compose only after adding the device reservation, and a reply pointed out that nvidia-smi inside the container proves visibility, not use: check ollama ps and GPU memory while generating, and confirm /dev/nvidia-uvm exists on the host (thread). Ollama’s GPU docs separately note that after a Linux suspend and resume, reloading nvidia_uvm brings the GPU back.

  • Mac users notice the CPU fallback quickly. A Hacker News commenter called losing Metal in Docker “a deal breaker” for local model work on a Mac (HN comment).

If the container starts but ollama ps still shows 100% CPU, the diagnosis is the same as outside Docker, and Ollama not using GPU walks through it.

FAQ

How do I update Ollama in Docker without losing models?

Pull the new image and recreate the container with the same volume: docker compose pull && docker compose up -d. Models live in the ollama volume, not the container, so they survive. Avoid docker compose down -v.

Where are Ollama models stored in Docker?

Inside the container they’re under /root/.ollama/models. On a Linux host, a named volume lives under Docker’s data root, typically /var/lib/docker/volumes/<name>/_data. Compose prefixes the volume with the project name (for example myproject_ollama), so run docker volume ls, then docker volume inspect <name> for the exact path.

How do I connect another container to Ollama in Docker Compose?

Put both services in the same Compose file and use the service name as the host: http://ollama:11434. From a container to Ollama running natively on the host, use http://host.docker.internal:11434.

Is the Ollama Docker image different for AMD GPUs?

Yes. ROCm needs the ollama/ollama:rocm tag. The default ollama/ollama image covers CPU, NVIDIA (CUDA) and Vulkan.

Does Ollama in Docker work on Windows?

Yes, through Docker Desktop with the WSL2 backend. The Ollama FAQ lists Linux and Windows with WSL2 as the platforms where GPU acceleration in Docker works with an NVIDIA card.

Sources (10)
  1. Docker, Ollama documentation
  2. FAQ (environment variables, proxy, GPU in Docker), Ollama documentation
  3. Hardware support (GPU), Ollama documentation
  4. Ollama Dockerfile (ENV and ENTRYPOINT), ollama/ollama on GitHub
  5. Context length, Ollama documentation
  6. Installing the NVIDIA Container Toolkit, NVIDIA documentation
  7. Enable GPU support, Docker Compose documentation
  8. Port publishing and mapping, Docker documentation
  9. M-series Macs running llama.cpp in GPU-accelerated containers, ggml-org/llama.cpp discussion #12985
  10. Default n_threads ignores cgroup CPU quota and cpuset, ollama/ollama issue #17916

Keep reading