关联问题:基于 Docker 的深度学习环境
关联问题:安装docker是否需要安装nvidia-docker以支持GPU?

结论

在基于continuumio/anaconda3镜像搭建完深度学习环境后,如何使用GPU让我产生了困扰。网上的教程有的说要用nvidia-docker,有的又说需要安装nvidia-container-toolkit,有的说安装nvidia-container-runtime,让人头大。

这里先直接说结论:docker版本19.03以后,安装nvidia-container-toolkit以及GPU驱动即可,对应的官方教程为:https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html,照着一步一步来就行。
官方也给出了示例:https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/sample-workload.html
此外,在Pytorch中使用nvidia GPU时,还需要添加指定参数:

--gpus all -e NVIDIA_DRIVER_CAPABILITIES=compute,utility 

完整指令可以参考:

docker run -itd --gpus all --name 容器名 -e NVIDIA_DRIVER_CAPABILITIES=compute,utility -e NVIDIA_VISIBLE_DEVICES=all 镜像名

至于具体如何搭建步骤以及参考文章如下:

  1. 安装GPU驱动
  2. 安装docker
  3. 安装nvidia-container-toolkit:https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
  4. 基于docker搭建conda深度学习环境:https://blog.csdn.net/qq_41204464/article/details/121635600
  5. 镜像制作:https://blog.csdn.net/qq_32101863/article/details/120344080

————————————————————————————————————————————

前因后果

组件关系

首先,需要知道这些组件自上而下的关系如下:

libnvidia-container
该软件包负责确保 GNU/Linux 容器设置为支持 Nvidia GPU,并进行相关的重要处理。它被设计为与容器运行时(container-runtime)无关,并提供一个明确定义的API和一个包装的命令行界面(CLI),不同的容器运行时可以调用该 API 将 NVIDIA GPU 支持注入到它们的容器中。

nvidia-container-toolkit
该软件包提供了一个脚本,用于实现 runC 预启动(hook)所需的接口。在容器创建后但尚未启动之前,runC会调用该脚本,并提供了访问与容器相关的config.json的权限(例如,此config.json)。然后,该脚本会从config.json中提取信息,并使用这些信息调用libnvidia-container CLI,并使用适当的标志集。其中最重要的标志之一是确定要将哪些特定的GPU设备分配给容器。

nvidia-contianer-runtime
这个软件包曾经是 runC 的一个完整分支,其中注入了 NVIDIA 特定的代码。然而,现在它只是对机器上原生runC的一个薄包装。它的功能是接受一个runC规范作为输入,将 nvidia-container-toolkit 脚本作为预启动(hook)注入其中,并调用原生的runC。它会将修改后的runC规范和该钩子一起传递给它。需要注意的是,这个软件包是针对 runC,而非Docker的。

nvidia-docker2
这个软件包是其中唯一与docker特定相关的软件包。它将与nvidia-container-runtime关联的脚本安装到docker的/etc/docker/daemon.json文件中。这样,您就可以运行(例如)docker run --runtime=nvidia … 来自动为您的容器添加GPU支持。它还安装了一个包装脚本(nvidia-docker),它可以在不需要每次都指定–runtime=nvidia的情况下调用原生的docker CLI。它还允许您在主机上设置一个环境变量(NV_GPU),以指定应该注入到容器中的GPU。

如何使用这些组件取决于所使用的容器运行时。

docker官方说明

根据docker官方文档:需要安装nvidia-container-runtime,
在这里插入图片描述

nvidia官方说明

但是nvidia官方说法为:

Make sure you have installed the NVIDIA driver for your Linux Distribution Note that you do not need to install the CUDA Toolkit on the host system, but the NVIDIA driver needs to be installed
确保已为您的Linux发行版安装NVIDIA驱动程序请注意,您不需要在主机系统上安装CUDA Toolkit,但需要安装NVIDIA驱动程序.

In the past the nvidia-docker2 and nvidia-container-runtime packages were also discussed as part of the NVIDIA container stack. These packages should be considered deprecated as their functionality has been merged with the nvidia-container-toolkit package. The packages may still be available to introduce dependencies on nvidia-container-toolkit and ensure that older workflows continue to function. For more information on these packages see the documentation archive for version older than v1.12.0.
在过去, nvidia-docker2 和 nvidia-container-runtime 包也被作为NVIDIA容器堆栈的一部分进行讨论。这些包应该被认为是不推荐使用的,因为它们的功能已经与 nvidia-container-toolkit 包合并。这些包可能仍然可以用于引入对 nvidia-container-toolkit 的依赖关系,并确保旧的工作流继续运行。有关这些软件包的更多信息,请参阅版本早于 v1.12.0 的文档存档。

Installing the nvidia-container-toolkit package is sufficient for all use cases. This package is continuously being enhanced with additional functionality and tools that simplify working with containers and NVIDIA devices.
安装 nvidia-container-toolkit 包就足够了。该软件包不断得到增强,增加了更多功能和工具,可简化容器和NVIDIA设备的使用。

综上所述,需要安装的为nvidia-container-toolkit

参考:
nvidia-docker杂记-天翼云开发者社区 - 天翼云
docker官方:Runtime options with Memory, CPUs, and GPUs
GitHub - NVIDIA/nvidia-container-toolkit: Build and run containers leveraging NVIDIA GPUs
Architecture Overview ‒ NVIDIA Container Toolkit 1.14.3 documentation
Installing the NVIDIA Container Toolkit ‒ NVIDIA Container Toolkit 1.14.3 documentation
基于 Docker 的深度学习环境:入门篇
docker使用GPU总结 - linhaifeng - 博客园

Logo

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

更多推荐