只在宿主机安装 cuda 时,在 docker 还无法利用 GPU,还需要安装 nvidia-container-toolkit
,安装步骤如下:
- 配置 apt 源
1 2 3 4
| 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
|
- 安装
1
| sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
|
- 使用
在启动容器时,使用命令 --gpus
1 2
| docker run -it --rm --gpus all ubuntu nvidia-smi docker run -it --rm --gpus '"device=0,2"' ubuntu nvidia-smi
|
参考:
- Resource constraints | Docker Docs
- Installing the NVIDIA Container Toolkit — NVIDIA Container Toolkit 1.16.2 documentation