Skip to content

Product docsGPU servers

Use your GPU in Docker containers: NVIDIA, AMD and Intel

Give Docker containers access to the GPU: install the NVIDIA Container Toolkit, pass AMD devices /dev/kfd and /dev/dri, and reserve GPUs in Docker Compose.

  • Intermediate
  • 20 min read
  • Updated
On this page
  1. Before you start
  2. NVIDIA: install the NVIDIA Container Toolkit
  3. NVIDIA GPUs in Docker Compose
  4. AMD: pass the ROCm devices
  5. Intel: pass the render devices
  6. Troubleshooting
  7. Next steps

Docker containers are isolated from the host's devices, so a GPU must be passed in on purpose. How depends on the GPU maker: NVIDIA uses the NVIDIA Container Toolkit, AMD and Intel GPUs are passed in as device files. This guide sets up both and shows the Compose syntax.

Before you start

NVIDIA: install the NVIDIA Container Toolkit

Add NVIDIA's repository with its signing key, then install the toolkit:

Bash
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -s -L 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

NVIDIA's guide pins an exact version of the four toolkit packages; installing nvidia-container-toolkit without a version installs the current one with its dependencies.

Register the NVIDIA runtime with Docker and restart Docker:

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

Verify with NVIDIA's sample workload:

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

The container prints the same nvidia-smi table as the host.

NVIDIA GPUs in Docker Compose

Reserve the GPU in the service definition:

YAML
services:
  app:
    image: ollama/ollama
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

Use count: all to give the service every GPU.

AMD: pass the ROCm devices

AMD GPUs need two device paths: /dev/kfd (the compute interface) and /dev/dri (one node per GPU). AMD's example:

Bash
docker run -it --rm --device /dev/kfd --device /dev/dri --security-opt seccomp=unconfined rocm/rocm-terminal bash

Inside the container, rocminfo lists the GPU. --security-opt seccomp=unconfined is optional; AMD recommends it for HPC workloads that use numactl.

In Compose:

YAML
services:
  app:
    image: rocm/rocm-terminal
    devices:
      - /dev/kfd
      - /dev/dri

Intel: pass the render devices

Intel GPUs are reached through /dev/dri. Pass it in and use an image that contains Intel's compute or media runtime for your GPU:

Bash
docker run --rm --device /dev/dri ubuntu ls -l /dev/dri

Troubleshooting

could not select device driver "" with capabilities: [[gpu]]. Docker does not know the NVIDIA runtime. Run sudo nvidia-ctk runtime configure --runtime=docker and restart Docker.

nvidia-container-cli: initialization error. The NVIDIA driver on the host is not working. Check nvidia-smi on the host first.

AMD: Permission denied on /dev/kfd. The user running the container's process lacks access to the devices. Run as root inside the container, or add the host's video and render group IDs with --group-add.

The application in the container uses the CPU. The image may not include GPU support, or it was started without the GPU flags. Check the image's documentation for the GPU tag or variant.

Next steps

Frequently asked questions

Do I need the CUDA toolkit on the host?

No. The host needs the NVIDIA driver and the NVIDIA Container Toolkit; CUDA libraries come inside the container images.

Can several containers share one GPU?

Yes. Several containers can use the same GPU at the same time; they share its memory, so make sure their models fit together.

Why does the container say no GPU was found?

Usually the GPU was not requested: add --gpus all for NVIDIA, or the --device options for AMD and Intel, or the GPU reservation in Compose.

Does this work with Podman?

The NVIDIA Container Toolkit also documents Podman through the Container Device Interface (CDI). The commands in this guide are for Docker Engine.

Generate Password

Please confirm