0%

手动挂载Nvidia显卡到docker容器中

在docker使用显卡往往需要nvidia的nvidia-container驱动,本文将叫你如何使用原生docker挂载显卡。
虽然在最新版的docker中已经内置了nvidia驱动,但是对于podman等其他容器管理来说,这套方法依旧是有意义的。

1
2
3
4
5
6
7
8
sudo docker run -it --rm \
--device /dev/nvidia-uvm:/dev/nvidia-uvm:rwm \
--device /dev/nvidia-uvm-tools:/dev/nvidia-uvm-tools:rwm \
--device /dev/nvidia0:/dev/nvidia0:rwm \
--device /dev/nvidiactl:/dev/nvidiactl:rwm \
-v /usr/bin/nvidia-smi:/usr/bin/nvidia-smi:ro \
-v /usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:ro \
ubuntu nvidia-smi

设备

需要映射到容器中的控制设备包括

  • /dev/nvidia-uvm
  • /dev/nvidia-uvm-tools
  • /dev/nvidiactl

需要映射到容器中的显卡

  • /dev/nvidia0
  • /dev/nvidia1

可以根据需要自行增减数量,显卡编号从0开始,自动增加,nvidia0/nvidia1/nvidia2等等

驱动

需要将nvidia驱动映射入容器

  • /usr/bin/nvidia-smi
  • /usr/lib/x86_64-linux-gnu

效果

对于我们一开始的命令,其输出是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sun Mar 29 06:58:45 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.33.01 Driver Version: 440.33.01 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce RTX 208... Off | 00000000:AF:00.0 Off | N/A |
| 0% 25C P8 15W / 260W | 0MiB / 11019MiB | 0% Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+