docker使用gpu设置

如果docker中需要用到GPU,在安装docker之后需要安装nvidia-docker2。
以下为离线安装nvidia-docker2的方法。
下载方法: 参考https://github.com/NVIDIA/nvidia-docker
在一台可以上网的机器上,配置apt源,用来下载deb包

  1. 更新源
    Add the package repositories

    1
    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
  2. 下载包

    1
    2
    3
    4
    5
    apt download libnvidia-container1
    apt download libnvidia-container-tools
    apt download nvidia-container-runtime-hook
    apt download nvidia-container-runtime
    apt download nvidia-docker2
  3. 分析依赖-确定安装顺序

    1
    2
    3
    apt show nvidia-container-toolkit
    apt show libnvidia-container-tools
    apt show libnvidia-container1
  4. 安装-把下载的包放到要安装的服务器中,安装安装

    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
  5. 重启docker

    1
    sudo systemctl restart docker
  6. docker run

    1
    docker run --net=host --gpus 'all,"capabilities=compute,utility"' -ti -v "$(pwd):/src" --shm-size 16G imagename:latest /bin/bash
  7. dockerfile and docker-compose
    dockerfile中添加:

    1
    2
    ENV NVIDIA_VISIBLE_DEVICES all
    ENV NVIDIA_DRIVER_CAPABILITIES compute,utility

如果使用docker-compose部署,添加deploy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
version: '3'

services:
sa:
image: imagename:latest
container_name: sa
environment:
TZ: Asia/Shanghai
entrypoint: /sa.sh
restart: always
volumes:
- /sa:/sa
- /usr/share/zoneinfo:/usr/share/zoneinfo
network_mode: host
deploy:
resources:
reservations:
devices:
- driver: "nvidia"
count: "all"
capabilities: ["gpu"]