Nvidia-docker/container-toolkit (Ollama GPU use case)

When using a computer with Nvidia graphics card, its suggested by Ollama to run Nvidia container toolkit (nvidia-docker) beforehand to make full use of the GPU: Ollama is now available as an official Docker image · Ollama Blog

This way I could make use of my GPU within Docker Desktop on a Win11 device to it’s full potential.

But this guide from nvidia is quite complex : Installing the NVIDIA Container Toolkit — NVIDIA Container Toolkit 1.17.3 documentation

How about if I install Cuda instead (CUDA Toolkit - Free Tools and Training | NVIDIA Developer), would it replace the need to install the NVIDIA container toolkit? Is the guide above from Ollama even outdated?

Maybe as last option use docker compose, which is integrated within docker desktop, through github for ollama?

I would appreciate any detailed elaboation on how this GPU configuration works, thank you in advance.

I doubt just installing Cuda would do the trick, because there is nothing to tell Ollama, especially in a container, to use it. Are you sure there isn’t an Ollama build for Windows you can just install that already had GPU support enabled?

Quick read in their docs and it sounds like the Windows install binary already includes it, so just install it like a normal application, didn’t worry about using docker.

Ollama now runs as a native Windows application, including NVIDIA and AMD Radeon GPU support.

My intention was really to manage multiple containers, so i already downloaded and installed docker desktop.

I am eager to know how to use docker desktop with Nvidia GPU, using an Ollama model.

Did a quick research (not testet)
=> looks like you just have to use a nvidia-ubuntu image and it should work

# Base image with NVIDIA support
FROM nvidia/cuda:12.0-runtime-ubuntu22.04

# Install necessary dependencies
RUN apt-get update && apt-get install -y \
    python3 python3-pip \
    && apt-get clean

# Install Ollama or other required software
RUN pip install ollama  # or equivalent installation step for Ollama

# Expose a port if Ollama runs as a service
EXPOSE 5000

# Set entrypoint
CMD ["ollama", "run"]

Actually it looks like this is the correct way, but I’m getting "failed to connect to nvidia.github.io port 443 error (timeout): gpg: no valid OpenPGP data found

I’m checking my firewall incase that’s the culprit.

Maybe this helps:



Docker Build with NVIDIA GPU on Windows

This guide explains how to resolve connectivity and GPG key issues when building Docker images on Windows with NVIDIA GPU support.


Prerequisites

  1. Internet Access: Ensure your machine has a stable internet connection.
  2. NVIDIA GPU Driver: Install the latest GPU drivers from NVIDIA’s website.
  3. Docker Desktop: Install Docker Desktop with WSL 2 integration.
  4. WSL 2: Set up Windows Subsystem for Linux 2.
  5. NVIDIA Container Toolkit: Install the NVIDIA Container Toolkit for GPU support.

Steps to Resolve GPG Key Error

1. Verify Internet Access

Open a Command Prompt or PowerShell and check connectivity to NVIDIA:

curl https://nvidia.github.io -v

If this fails, check your network settings, firewall, or proxy.


2. Configure Docker for NVIDIA GPUs on Windows

  1. Install NVIDIA GPU Driver:

  2. Install NVIDIA Container Toolkit in WSL:

    • Launch WSL and follow the Linux installation steps:
      curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit.gpg
      
      curl -fsSL https://nvidia.github.io/libnvidia-container/stable/ubuntu20.04/libnvidia-container.list | \
      sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit.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
      

Replace ubuntu20.04 with your version of Ubuntu if different.


3. Fix GPG Key Issues in WSL

Manually add the GPG key and NVIDIA repository:

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit.gpg

curl -fsSL https://nvidia.github.io/libnvidia-container/stable/ubuntu20.04/libnvidia-container.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit.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

4. Configure Proxy for Docker (If Applicable)

If you’re behind a proxy, configure Docker Desktop to use proxy settings:

  1. Open Docker Desktop and go to SettingsResourcesProxies.
  2. Set the HTTP Proxy and HTTPS Proxy fields.

Alternatively, add proxy settings to your Docker config.json:

{
  "proxies": {
    "default": {
      "httpProxy": "http://proxy-server:port",
      "httpsProxy": "http://proxy-server:port"
    }
  }
}

Save this file to:

  • C:\Users\<YourUsername>\.docker\config.json

Restart Docker Desktop after making these changes.


5. Use an NVIDIA-Compatible Base Image

Ensure your Dockerfile uses an NVIDIA-compatible base image:

FROM nvidia/cuda:12.0-runtime-ubuntu22.04

6. Retry the Build

Run the Docker build command from PowerShell or WSL:

docker build -t ollama-gpu .

7. Debugging Docker Issues on Windows

If issues persist, check the logs:

  • Docker Logs:

    docker logs <container-id>
    
  • Restart Docker Desktop:

    Restart-Service com.docker.service
    

Additional Notes

  • Ensure that your Docker Desktop installation is configured to use WSL 2.
  • Verify that the nvidia-smi command works in WSL to confirm GPU availability.
  • Test connectivity to nvidia.github.io to ensure proper access.

This guide should help resolve GPG key and connectivity issues during Docker builds on Windows with NVIDIA GPU support.

1 Like

Very useful, thank you.

But for the ‘curl’ commands before “apt-get” to finally install the NVIDIA Container Toolkit in WSL, I see that your script is different to the one shown in Ollama. Also the Ollama docs didnt state that “APT” is a package manager purely for debian-based Linux distributions and cannot be run in cmd or powershell (not mentioned, see screenshot below):

´curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit.gpg

curl -fsSL https://nvidia.github.io/libnvidia-container/stable/ubuntu20.04/libnvidia-container.list |
sed ‘s#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit.gpg] https://#g’ |
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list´

but you should be able to run in WSL

1 Like

No I cant Configure Docker to use Nvidia driver

The other steps were successful.

Just restart your docker manually (close the desktop application and reopen)

same error

I moved onto next step to start the container and it’s pulling some data:

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

1 Like

I do now see Ollama in docker desktop, but the following commands still give errors:

sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

docker compose --profile gpu-nvidia up

I assume I dont have to start the container command every time in bash and docker desktop does this automatically?