title: Docker make Nvidia GPUs visible during docker build process
body: <p>I want to build a docker image where I want to compile custom kernels with pytorch. Therefore I need access to the available gpus in order to compile the custom kernels during docker build process. On the host machine everything is setted up including <code>nvidia-container-runtime</code>, <code>nvidia-docker</code>, Nvidia-Drivers, Cuda etc. The following command shows docker runtime information on the host system:</p>
  <pre><code>$ docker info|grep -i runtime
   Runtimes: nvidia runc
   Default Runtime: runc
  </code></pre>
  <p>As you can see the default runtime of docker in my case is <code>runc</code>. I think changing the default runtime from <code>runc</code> to <code>nvidia</code> would solve this problem, as noted <a href="https://stackoverflow.com/questions/59691207/docker-build-with-nvidia-runtime">here</a>.</p>
  <p>The proposed solution doesn't work in my case because:</p>
  <ul>
  <li>I have no permissions to change the default runtime on system I use</li>
  <li>I have no permissions to make changes to the <code>daemon.json</code> file</li>
  </ul>
  <p>Is there a way to get access to the gpus during the build process in the Dockerfile in order to compile custom pytorch kernels for CPU and GPU (in my case DCNv2)?</p>
  <p>Here is the minimal example of my Dockerfile to reproduce this problem. In this image, DCNv2 is only compiled for CPU and not for GPU.</p>
  <pre><code>FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu18.04
  
  RUN apt-get update &amp;&amp; \
      DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata &amp;&amp; \
      apt-get install -y --no-install-recommends software-properties-common &amp;&amp; \
      add-apt-repository ppa:deadsnakes/ppa &amp;&amp; \
      apt update &amp;&amp; \
      apt install -y --no-install-recommends python3.6 &amp;&amp; \
      apt-get install -y --no-install-recommends \
      build-essential \
      python3.6-dev \
      python3-pip \
      python3.6-tk \
      pkg-config \
      software-properties-common \
      git
  
  RUN ln -s /usr/bin/python3 /usr/bin/python &amp; \
      ln -s /usr/bin/pip3 /usr/bin/pip
  
  RUN python -m pip install --no-cache-dir --upgrade pip setuptools &amp;&amp; \
      python -m pip install --no-cache-dir torch==1.4.0 torchvision==0.5.0
  
  RUN git clone https://github.com/CharlesShang/DCNv2/
  
  #Compile DCNv2
  WORKDIR /DCNv2
  RUN bash ./make.sh
  
  # clean up
  RUN apt-get clean &amp;&amp; \
      rm -rf /var/lib/apt/lists/*
  
  #Build: docker build -t my_image .
  #Run: docker run -it my_image
  </code></pre>
  <p>An not opitmal solution which worked would be be the following:</p>
  <ul>
  <li>Comment out line <code>RUN bash ./make.sh</code> in Dockerfile</li>
  <li>Build image: <code>docker build -t my_image .</code></li>
  <li>Run image in interactive mode: <code>docker run --gpus all -it my_image</code></li>
  <li>Compile DCNv2 manually: <code>root@1cd02fd62461:/DCNv2# ./make.sh</code></li>
  </ul>
  <p>Here DCNv2 is compiled for CPU and GPU, but that seems to me not an ideal solution, because I must compile DCNv2 every time when i start the container.</p>