This page is also available in: English
宿主机的 Nvidia 显卡无法直接用于 Docker 容器,必须要先安装 NVIDIA Container Toolkit 才可以将显卡映射到容器中。
下面是一条命令,完整复制并在终端执行即可。
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
curl -s -L https://nvidia.github.io/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo | \
sudo tee /etc/yum.repos.d/nvidia-container-toolkit.repo
sudo yum install -y nvidia-container-toolkit
为了让容器可以调用宿主机上的 Nvidia 显卡,还需要对 Docker 容器运行时进行配置,使用以下命令:
sudo nvidia-ctk runtime configure --runtime=docker
这个命令会自动更新 /etc/docker/daemon.json
配置文件。
重启 Docker 服务:
sudo systemctl restart docker
经过上述安装和配置以后,Docker 容器就可以使用宿主机上的 Nvidia 显卡了,下面是一个使用示例:
sudo docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi
容器启动后会打印 GPU 运行状态信息,类似下面这样:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 535.86.10 Driver Version: 535.86.10 CUDA Version: 12.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
| N/A 34C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
更多内容请参考 https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/index.html
本文发布于 2024-08-20,最近更新 2024-09-23。
本文版权归 torchtree.com 网站所有,未经授权不可转载。