Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Install Ollama Docker GPU: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. Why Run Ollama in Docker?
  2. Prerequisites
  3. Step-by-Step: NVIDIA GPU
  4. Step-by-Step: AMD GPU
  5. Verifying GPU Usage
  6. Common Problems and Fixes
  7. Docker vs Native Install
  8. Frequently Asked Questions

Yes, you can install Ollama in Docker with GPU support by using the official ollama/ollama image and passing your GPU through with the --gpus flag (for NVIDIA) or the appropriate device mapping (for AMD). This guide walks you through the exact steps, prerequisites, and common pitfalls.

Why Run Ollama in Docker?

Docker packaging gives you a clean, reproducible environment for running local LLMs. Instead of installing Ollama directly on your host OS, you get:

  • Isolation – the runtime, model files, and dependencies stay inside the container, so they won't clash with other software.
  • Easy upgrades – pulling a new image replaces the whole runtime, not just a binary.
  • Portability – the same container runs on any Linux machine with Docker and a compatible GPU.
  • Simplified cleanup – removing the container and image removes everything Ollama-related.

That said, Docker adds a layer of abstraction. If you're only running Ollama on one machine and never plan to move it, a native install might be simpler. But for multi-service setups, CI/CD, or when you want to keep the host clean, Docker is a solid choice.

Prerequisites

Before you start, make sure you have:

  • Docker Engine installed (20.10+ recommended). On Windows or macOS, that means Docker Desktop.
  • An NVIDIA GPU with a recent driver (for CUDA) or an AMD GPU with the ROCm stack.
  • For NVIDIA: the NVIDIA Container Toolkit (nvidia-container-toolkit) installed and configured. This is what lets Docker see your GPU.
  • For AMD: the ROCm drivers and a Docker runtime that supports device mapping (usually just --device).
  • Enough RAM and disk space – models like Llama 3.1 8B need about 8 GB of VRAM and 5 GB of disk for the model itself.

Step-by-Step: NVIDIA GPU

Here's the typical flow for an NVIDIA GPU on Linux (the most common setup).

  1. Install the NVIDIA Container Toolkit – follow the official instructions for your distribution. The key command is usually sudo apt-get install -y nvidia-container-toolkit after adding the repo, then sudo systemctl restart docker.
  2. Pull the Ollama image: docker pull ollama/ollama
  3. Run the container with GPU access:
    docker run -d --gpus all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
    The --gpus all flag exposes all your NVIDIA GPUs to the container. The -v ollama:/root/.ollama creates a named volume to persist models across container restarts.
  4. Pull a model – inside the container or from the host using the API:
    docker exec -it ollama ollama pull llama3.1
  5. Test it – run a quick prompt: docker exec -it ollama ollama run llama3.1

If you get a could not select device driver error, that means the NVIDIA Container Toolkit isn't installed or Docker wasn't restarted after installation.

Step-by-Step: AMD GPU

AMD support is less turnkey. Ollama's official image includes ROCm support, but you need to pass the GPU device explicitly.

  1. Install the ROCm drivers for your AMD GPU (see the ROCm documentation for your distro).
  2. Find your GPU device number with rocm-smi or lspci | grep -i amd.
  3. Run the container with --device mapping, for example:
    docker run -d --device=/dev/kfd --device=/dev/dri -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
  4. Pull and test a model as above.

Note that AMD support is evolving; some older GPUs may not work. Check the Ollama GitHub issues for your specific model.

Verifying GPU Usage

How do you know the GPU is actually being used? The easiest way is to watch GPU utilization while running a prompt.

  • On the host, run nvidia-smi (NVIDIA) or rocm-smi (AMD) in a separate terminal while you send a request.
  • If you see a process using VRAM and high compute % during inference, it's working.
  • Alternatively, run docker exec -it ollama nvidia-smi (NVIDIA) – if it shows your GPU, the container sees it.

If the GPU isn't used, Ollama will fall back to CPU, which is much slower. You'll notice the difference immediately.

Common Problems and Fixes

Here are the most frequent issues people hit and how to solve them.

  • “could not select device driver” – NVIDIA Container Toolkit not installed or Docker not restarted. Reinstall the toolkit and run sudo systemctl restart docker.
  • Container runs but no GPU – Check that you passed --gpus all (NVIDIA) or the correct --device flags (AMD). Also verify the toolkit is working with docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi.
  • Out of memory – Model too big for your VRAM. Choose a smaller model or use quantization (e.g., llama3.1:8b-instruct-q4_0).
  • Slow inference – Could be CPU fallback, or your GPU is old. Check utilization as above.
  • Models not persisting – You must mount a volume to /root/.ollama; otherwise models are lost when the container is removed.

Docker vs Native Install

Which is better? It depends on your priorities.

  • Docker – Great for isolation, easy rollback, and multi-service setups. Slight overhead (a few percent) and an extra learning curve if you're not familiar with Docker.
  • Native – Simpler for a single machine, slightly faster, and no container runtime to manage. But upgrading is messier, and uninstalling leaves files behind.

For most users, native is fine. For developers and homelab enthusiasts, Docker is often cleaner. There's no right answer – pick what fits your workflow.

Frequently Asked Questions

Do I need a GPU to run Ollama in Docker?

No, you can run Ollama without a GPU – it will use your CPU. But inference will be significantly slower, especially for larger models. A GPU is strongly recommended for any serious use.

Can I use Docker on Windows or macOS with GPU?

On Windows, Docker Desktop supports NVIDIA GPUs via WSL2, but it requires the NVIDIA driver and the WSL2 backend. On macOS, GPU passthrough is not supported for NVIDIA; Apple Silicon users can use the native Ollama app, but Docker GPU support is not available. For AMD on Windows, support is limited.

How do I update Ollama in Docker?

Pull the latest image: docker pull ollama/ollama, then recreate the container with the same run command. Your models persist in the volume.

Is it safe to run Ollama in Docker?

Yes, as long as you keep the container updated and don't expose the API port (11434) to the public internet without authentication. By default, the API has no auth, so bind it to localhost only (e.g., -p 127.0.0.1:11434:11434).

What if my GPU is not detected?

First, verify the GPU works on the host (run nvidia-smi). Then check the container toolkit. For AMD, ensure the kernel modules are loaded. If it still fails, search the Ollama GitHub issues for your GPU model.