使用来自 docker 容器的 GPU? [英] Using GPU from a docker container?
问题描述
我正在寻找一种从 docker 容器内部使用 GPU 的方法.
I'm searching for a way to use the GPU from inside a docker container.
容器将执行任意代码,所以我不想使用特权模式.
The container will execute arbitrary code so i don't want to use the privileged mode.
有什么建议吗?
从之前的研究中我了解到 run -v
和/或 LXC cgroup
是要走的路,但我不确定如何完全实现>
From previous research i understood that run -v
and/or LXC cgroup
was the way to go but i'm not sure how to pull that off exactly
推荐答案
写一个更新的答案,因为大多数已经存在的答案现在已经过时了.
Writing an updated answer since most of the already present answers are obsolete as of now.
早于 Docker 19.03
的版本过去需要 nvidia-docker2
和 --runtime=nvidia
标志.
Versions earlier than Docker 19.03
used to require nvidia-docker2
and the --runtime=nvidia
flag.
从Docker 19.03
开始,您需要安装nvidia-container-toolkit
包,然后使用--gpus all
标志.
Since Docker 19.03
, you need to install nvidia-container-toolkit
package and then use the --gpus all
flag.
所以,这是基础知识,
安装包
根据 Github 官方文档安装 nvidia-container-toolkit
包.
对于基于 Redhat 的操作系统,执行以下命令集:
For Redhat based OSes, execute the following set of commands:
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.repo | sudo tee /etc/yum.repos.d/nvidia-docker.repo
$ sudo yum install -y nvidia-container-toolkit
$ sudo systemctl restart docker
对于基于 Debian 的操作系统,执行以下命令集:
For Debian based OSes, execute the following set of commands:
# Add the package repositories
$ 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
$ sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
$ sudo systemctl restart docker
在 GPU 支持下运行 docker
docker run --name my_all_gpu_container --gpus all -t nvidia/cuda
请注意,标志 --gpus all
用于将所有可用的 gpu 分配给 docker 容器.
Please note, the flag --gpus all
is used to assign all available gpus to the docker container.
将特定的 gpu 分配给 docker 容器(如果您的机器中有多个 GPU)
To assign specific gpu to the docker container (in case of multiple GPUs available in your machine)
docker run --name my_first_gpu_container --gpus device=0 nvidia/cuda
或
docker run --name my_first_gpu_container --gpus '"device=0"' nvidia/cuda
这篇关于使用来自 docker 容器的 GPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!