951

Ubuntu22.04 DockerCE 容器内使用GPU

注意只有DockerCE版本才能使用GPU
如果教程无法使用了,请参考官方地址:nvidia-container-toolkit

安装nvidia软件源

distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

or

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
sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt update

安装

sudo apt install nvidia-container-toolkit

检查是否成功

sudo which nvidia-container-runtime-hook

如果找到nvidia-container-runtime-hook则安装成功了

重启Docker

sudo systemctl restart docker

测试使用

docker run --gpus all nvidia/cuda:latest nvidia-smi

显卡监控

docker run -d --gpus all -p 9200:9400 nvcr.io/nvidia/k8s/dcgm-exporter:3.3.5-3.4.0-ubuntu22.04