docker使用gpu设置
如果docker中需要用到GPU,在安装docker之后需要安装nvidia-docker2。
以下为离线安装nvidia-docker2的方法。
下载方法: 参考https://github.com/NVIDIA/nvidia-docker
在一台可以上网的机器上,配置apt源,用来下载deb包
更新源
Add the package repositories1
2
3
4$ 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下载包
1
2
3
4
5apt download libnvidia-container1
apt download libnvidia-container-tools
apt download nvidia-container-runtime-hook
apt download nvidia-container-runtime
apt download nvidia-docker2分析依赖-确定安装顺序
1
2
3apt show nvidia-container-toolkit
apt show libnvidia-container-tools
apt show libnvidia-container1安装-把下载的包放到要安装的服务器中,安装安装
1
2
3
4
5
6# 推荐
dpkg -i libnvidia* nvidia*
# 或者以下
sudo dpkg -i libnvidia-container1_1.4.0-1_amd64.deb
sudo dpkg -i libnvidia-container-tools_1.4.0-1_amd64.deb
sudo dpkg -i nvidia-container-toolkit_1.5.0-1_amd64.deb重启docker
1
sudo systemctl restart docker
docker run
1
docker run --net=host --gpus 'all,"capabilities=compute,utility"' -ti -v "$(pwd):/src" --shm-size 16G imagename:latest /bin/bash
dockerfile and docker-compose
dockerfile中添加:1
2ENV NVIDIA_VISIBLE_DEVICES all
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
如果使用docker-compose部署,添加deploy:
1 | version: '3' |