Windows系统下在docker中使用nvidia的GPU

微软官方文档

https://docs.microsoft.com/ja-jp/windows/ai/directml/gpu-cuda-in-wsl

参考nvidia官方文档

https://docs.nvidia.com/cuda/wsl-user-guide/index.html#getting-started-with-cuda-on-wsl

在windows系统中安装显卡驱动

注意,请更新到最新的驱动,老版本的驱动可能报错:

docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error running hook #0: error running hook: exit status 1, stdout: , stderr: Auto-detected mode as 'legacy'
nvidia-container-cli: initialization error: WSL environment detected but no adapters were found: unknown

安装wsl2

按照微软官方教程(https://docs.microsoft.com/en-us/windows/wsl/install),在power shell中输入

wsl --install

最后可以检查一下wsl安装版本,需求wsl2,在power shell中输入

wsl cat /proc/version

应当有如下输出

Linux version 5.10.16.3-microsoft-standard-WSL2 (oe-user@oe-host) (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) 2.34.0.20200220) #1 SMP Fri Apr 2 22:23:49 UTC 2021

安装Docker

按照Docker官方网站安装Docker Desktop(Docker Engine也可以)

安装NVIDIA Container Toolkit

(https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker)

添加软件源

distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
      && 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/$distribution/libnvidia-container.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

安装nvidia-docker-2

sudo apt-get install -y nvidia-docker2

重启docker(或者直接在docker desktop中重启)

sudo systemctl restart docker

测试是否成功(–gpus all表示容器可以使用所有的GPU)

sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi

应该能正常显示显卡属性等信息,如果有问题请检查之前几步是否版本有问题

至此,Windows上也能愉快的在docker中使用GPU啦

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐