IT박스

도커 컨테이너에서 GPU를 사용하십니까?

itboxs 2020. 6. 24. 07:50
반응형

도커 컨테이너에서 GPU를 사용하십니까?


도커 컨테이너 내부에서 GPU를 사용하는 방법을 찾고 있습니다.

컨테이너는 임의의 코드를 실행하므로 권한 모드를 사용하고 싶지 않습니다.

팁이 있습니까?

이전 연구에서 나는 run -vLXC cgroup가 갈 길이 라는 것을 이해 했지만 정확히 그것을 뽑는 방법을 모르겠습니다.


Regan의 대답은 훌륭하지만 조금 오래되었습니다 .Docker가 docker 0.9에서 LXC 를 기본 실행 컨텍스트로 삭제 했기 때문에 올바른 방법은 lxc 실행 컨텍스트를 피하는 것 입니다.

대신 --device 플래그를 통해 docker에게 nvidia 장치에 대해 알리고 lxc 대신 기본 실행 컨텍스트를 사용하는 것이 좋습니다.

환경

이 지침은 다음 환경에서 테스트되었습니다.

  • 우분투 14.04
  • CUDA 6.5
  • AWS GPU 인스턴스.

호스트에 nvidia 드라이버 및 cuda 설치

호스트 시스템 설정을 얻으려면 Ubuntu 14.04실행하는 AWS GPU 인스턴스의 CUDA 6.5를 참조하십시오 .

Docker 설치

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
$ sudo sh -c "echo deb https://get.docker.com/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update && sudo apt-get install lxc-docker

엔비디아 기기 찾기

ls -la /dev | grep nvidia

crw-rw-rw-  1 root root    195,   0 Oct 25 19:37 nvidia0 
crw-rw-rw-  1 root root    195, 255 Oct 25 19:37 nvidiactl
crw-rw-rw-  1 root root    251,   0 Oct 25 19:37 nvidia-uvm

nvidia 드라이버가 사전 설치된 Docker 컨테이너 실행

cuda 드라이버가 사전 설치된 도커 이미지만들었습니다 . dockerfile는 이 이미지가 구축 된 방법을 알고 싶다면 dockerhub 볼 수 있습니다.

nvidia 장치와 일치하도록이 명령을 사용자 정의하려고합니다. 나를 위해 일한 것은 다음과 같습니다.

 $ sudo docker run -ti --device /dev/nvidia0:/dev/nvidia0 --device /dev/nvidiactl:/dev/nvidiactl --device /dev/nvidia-uvm:/dev/nvidia-uvm tleyden5iwx/ubuntu-cuda /bin/bash

CUDA가 올바르게 설치되었는지 확인

방금 시작한 도커 컨테이너 내부에서 실행해야합니다.

CUDA 샘플 설치 :

$ cd /opt/nvidia_installers
$ ./cuda-samples-linux-6.5.14-18745345.run -noprompt -cudaprefix=/usr/local/cuda-6.5/

deviceQuery 샘플을 빌드하십시오.

$ cd /usr/local/cuda/samples/1_Utilities/deviceQuery
$ make
$ ./deviceQuery   

모든 것이 작동하면 다음과 같은 결과가 나타납니다.

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 6.5, CUDA Runtime Version = 6.5, NumDevs =    1, Device0 = GRID K520
Result = PASS

Ok --privileged 모드를 사용하지 않고 마침내 관리했습니다.

우분투 서버 14.04에서 실행 중이며 최신 cuda (리눅스 13.04 64 비트의 경우 6.0.37)를 사용하고 있습니다.


예비

호스트에 nvidia 드라이버 및 cuda를 설치하십시오. (조금 까다로울 수 있으므로이 가이드 https://askubuntu.com/questions/451672/installing-and-testing-cuda-in-ubuntu-14-04 을 따르십시오. )

주의 : 호스트 cuda 설치에 사용한 파일을 보관하는 것이 중요합니다


lxc를 사용하여 Docker 데몬 실행

구성을 수정하고 컨테이너에 장치에 대한 액세스 권한을 부여하려면 lxc 드라이버를 사용하여 docker 데몬을 실행해야합니다.

한 번 활용 :

sudo service docker stop
sudo docker -d -e lxc

영구 구성 / etc / default / docker에있는 도커 구성 파일 수정 '-e lxc'를 추가하여 DOCKER_OPTS 행을 변경하십시오. 수정 후 내 행은 다음과 같습니다.

DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 -e lxc"

그런 다음 다음을 사용하여 데몬을 다시 시작하십시오.

sudo service docker restart

데몬이 효과적으로 lxc 드라이버를 사용하는지 확인하는 방법은 무엇입니까?

docker info

실행 드라이버 줄은 다음과 같아야합니다.

Execution Driver: lxc-1.0.5

NVIDIA 및 CUDA 드라이버로 이미지를 만드십시오.

다음은 CUDA 호환 이미지를 빌드하기위한 기본 Dockerfile입니다.

FROM ubuntu:14.04
MAINTAINER Regan <http://stackoverflow.com/questions/25185405/using-gpu-from-a-docker-container>

RUN apt-get update && apt-get install -y build-essential
RUN apt-get --purge remove -y nvidia*

ADD ./Downloads/nvidia_installers /tmp/nvidia                             > Get the install files you used to install CUDA and the NVIDIA drivers on your host
RUN /tmp/nvidia/NVIDIA-Linux-x86_64-331.62.run -s -N --no-kernel-module   > Install the driver.
RUN rm -rf /tmp/selfgz7                                                   > For some reason the driver installer left temp files when used during a docker build (i don't have any explanation why) and the CUDA installer will fail if there still there so we delete them.
RUN /tmp/nvidia/cuda-linux64-rel-6.0.37-18176142.run -noprompt            > CUDA driver installer.
RUN /tmp/nvidia/cuda-samples-linux-6.0.37-18176142.run -noprompt -cudaprefix=/usr/local/cuda-6.0   > CUDA samples comment if you don't want them.
RUN export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64         > Add CUDA library into your PATH
RUN touch /etc/ld.so.conf.d/cuda.conf                                     > Update the ld.so.conf.d directory
RUN rm -rf /temp/*  > Delete installer files.

이미지를 실행하십시오.

먼저 장치와 관련된 주요 번호를 식별해야합니다. 가장 쉬운 방법은 다음 명령을 수행하는 것입니다.

ls -la /dev | grep nvidia

If the result is blank, use launching one of the samples on the host should do the trick. The result should look like that enter image description here As you can see there is a set of 2 numbers between the group and the date. These 2 numbers are called major and minor numbers (wrote in that order) and design a device. We will just use the major numbers for convenience.

Why do we activated lxc driver? To use the lxc conf option that allow us to permit our container to access those devices. The option is : (i recommend using * for the minor number cause it reduce the length of the run command)

--lxc-conf='lxc.cgroup.devices.allow = c [major number]:[minor number or *] rwm'

So if i want to launch a container (Supposing your image name is cuda).

docker run -ti --lxc-conf='lxc.cgroup.devices.allow = c 195:* rwm' --lxc-conf='lxc.cgroup.devices.allow = c 243:* rwm' cuda

We just released an experimental GitHub repository which should ease the process of using NVIDIA GPUs inside Docker containers.


Recent enhancements by NVIDIA have produced a much more robust way to do this.

Essentially they have found a way to avoid the need to install the CUDA/GPU driver inside the containers and have it match the host kernel module.

Instead, drivers are on the host and the containers don't need them. It requires a modified docker-cli right now.

This is great, because now containers are much more portable.

enter image description here

A quick test on Ubuntu:

# Install nvidia-docker and nvidia-docker-plugin
wget -P /tmp https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.1/nvidia-docker_1.0.1-1_amd64.deb
sudo dpkg -i /tmp/nvidia-docker*.deb && rm /tmp/nvidia-docker*.deb

# Test nvidia-smi
nvidia-docker run --rm nvidia/cuda nvidia-smi

For more details see: GPU-Enabled Docker Container and: https://github.com/NVIDIA/nvidia-docker


Updated for cuda-8.0 on ubuntu 16.04

Dockerfile

FROM ubuntu:16.04
MAINTAINER Jonathan Kosgei <jonathan@saharacluster.com>

# A docker container with the Nvidia kernel module and CUDA drivers installed

ENV CUDA_RUN https://developer.nvidia.com/compute/cuda/8.0/prod/local_installers/cuda_8.0.44_linux-run

RUN apt-get update && apt-get install -q -y \
  wget \
  module-init-tools \
  build-essential 

RUN cd /opt && \
  wget $CUDA_RUN && \
  chmod +x cuda_8.0.44_linux-run && \
  mkdir nvidia_installers && \
  ./cuda_8.0.44_linux-run -extract=`pwd`/nvidia_installers && \
  cd nvidia_installers && \
  ./NVIDIA-Linux-x86_64-367.48.run -s -N --no-kernel-module

RUN cd /opt/nvidia_installers && \
  ./cuda-linux64-rel-8.0.44-21122537.run -noprompt

# Ensure the CUDA libs and binaries are in the correct environment variables
ENV LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64
ENV PATH=$PATH:/usr/local/cuda-8.0/bin

RUN cd /opt/nvidia_installers &&\
    ./cuda-samples-linux-8.0.44-21122537.run -noprompt -cudaprefix=/usr/local/cuda-8.0 &&\
    cd /usr/local/cuda/samples/1_Utilities/deviceQuery &&\ 
    make

WORKDIR /usr/local/cuda/samples/1_Utilities/deviceQuery
  1. Run your container

sudo docker run -ti --device /dev/nvidia0:/dev/nvidia0 --device /dev/nvidiactl:/dev/nvidiactl --device /dev/nvidia-uvm:/dev/nvidia-uvm <built-image> ./deviceQuery

You should see output similar to:

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GRID K520 Result = PASS


To use GPU from docker container, instead of using native Docker, use Nvidia-docker. To install Nvidia docker use following commands

curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey |  sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64/nvidia-
docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker
sudo pkill -SIGHUP dockerd # Restart Docker Engine
sudo nvidia-docker run --rm nvidia/cuda nvidia-smi # finally run nvidia-smi in the same container

Use x11docker by mviereck:

https://github.com/mviereck/x11docker#hardware-acceleration says

Hardware acceleration

Hardware acceleration for OpenGL is possible with option -g, --gpu.

This will work out of the box in most cases with open source drivers on host. Otherwise have a look at wiki: feature dependencies. Closed source NVIDIA drivers need some setup and support less x11docker X server options.

This script is really convenient as it handles all the configuration and setup. Running a docker image on X with gpu is as simple as

x11docker --gpu imagename

참고URL : https://stackoverflow.com/questions/25185405/using-gpu-from-a-docker-container

반응형