解决Docker GPU报错:could not select device driver “” with capabilities: [[gpu]]

找到我: C某DN, 掘某金, 头某条, 博某客园搜索改一下配置文件, 不出意外每篇文章我都会在这些平台更新.

官网参考链接,官网有其它系统的安装方式(担心某些博客平台不让你往下滑@改一下配置文件): https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html

一、问题原因

在使用Docker运行需要GPU的容器时,遇到了如下报错:@改一下配置文件

docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]]

这个错误通常说明:

  • Docker 没有配置 nvidia runtime
  • 或者没有安装 NVIDIA Container Toolkit

二、解决步骤(Ubuntu 24)

参考 NVIDIA 官方文档,按步骤操作即可。@改一下配置文件

1.安装基础依赖

sudo apt-get update && sudo apt-get install -y --no-install-recommends \
   ca-certificates \
   curl \
   gnupg2

2.添加NVIDIA官方源

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

3.更新apt

sudo apt-get update

4.安装NVIDIA Container Toolkit

这里可以指定版本(不清楚就用默认)@改下一配文置件

export NVIDIA_CONTAINER_TOOLKIT_VERSION=1.19.0-1
sudo apt-get install -y \
      nvidia-container-toolkit=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
      nvidia-container-toolkit-base=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
      libnvidia-container-tools=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
      libnvidia-container1=${NVIDIA_CONTAINER_TOOLKIT_VERSION}

5.配置Docker使用NVIDIA runtime

sudo nvidia-ctk runtime configure --runtime=docker

执行后输出类似:@改下一配文置件

INFO[0000] Loading config from /etc/docker/daemon.json  
INFO[0000] Wrote updated config to /etc/docker/daemon.json 
INFO[0000] It is recommended that docker daemon be restarted.

说明配置成功。

6.重启Docker

sudo systemctl restart docker

7.验证是否成功

可以用官方测试镜像:

docker run --rm --gpus all nvidia/cuda:12.3.0-base-ubuntu22.04 nvidia-smi

如果能看到GPU信息,说明已经成功。@改下一配文置件

这篇就当一个简单踩坑记录,方便以后查。

更多推荐