02 docker安装
要在Docker容器中使用NVIDIA GPU,需要安装NVIDIA Container Toolkit。以下是安装步骤:
确保您的系统上已安装NVIDIA驱动程序,并且您可以在主机上运行nvidia-smi命令。
确保您的系统上已安装Docker。如果没有,请先安装Docker。
安装NVIDIA Container Toolkit:
- 通过添加NVIDIA的APT存储库来安装NVIDIA Container Toolkit:
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L docker/gpgkey | sudo apt-key add -
$ curl -s -L docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
$ sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
$ sudo systemctl restart docker
- 或者,通过在Dockerfile中添加以下指令来安装NVIDIA Container Toolkit:
# 安装NVIDIA Container Toolkit
ENV NVIDIA_VISIBLE_DEVICES all
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
RUN curl -s -L docker/gpgkey | apt-key add -
RUN distribution=$(. /etc/os-release;echo $ID$VERSION_ID) && \
curl -s -L docker/$distribution/nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list && \
apt-get update && apt-get install -y nvidia-container-toolkit && \
systemctl restart docker
- 确认NVIDIA Container Toolkit已正确安装:
$ docker run --gpus all nvidia/cuda:11.0-base nvidia-smi
如果您看到类似以下内容的输出,则表示已成功安装:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.57.02 Driver Version: 470.57.02 CUDA Version: 11.4 |
|-------------------------------+----------------------+----------------------+
| 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 P100-PCIE... On | 00000000:00:04.0 Off | 0 |
| N/A 29C P0 31W / 250W | 0MiB / 16280MiB | 0% |
| | | N/A |
+-------------------------------+----------------------+----------------------+
现在,您可以在Docker容器中使用NVIDIA GPU了。