title: Using OpenGL inside docker with nvidia-docker2
body: <p>I'm trying to run OpenGL applications (<a href="http://gazebosim.org" rel="noreferrer">Gazebo</a>) inside a Ubuntu 16.04 container, and I'd like to be able to take advantage of nvidia graphics acceleration when available. I'm trying to figure out what the recommended, officially supported (by nvidia, hopefully) way to achieve this is.</p>
  
  <p>My requirements:</p>
  
  <ol>
  <li>Creating the image is time-consuming, so I'd like to either have one image for all kinds of graphics (nvidia, mesa i.e. everything else), or if separate, they should be built "FROM" a common base image with the bulk of the content.</li>
  <li>The nvidia container should work on different systems which may have different nvidia cards and driver versions installed.</li>
  <li>I need to use Ubuntu 16.04, company requires this, although this is the least-important of these requirements, e.g. if this could only be done on 18.04 I'd be interested as well.</li>
  </ol>
  
  <p>What I've tried so far:</p>
  
  <ul>
  <li>Just build separate images for nvidia and everything else, with <code>FROM nvidia/opengl:1.0-glvnd-runtime-ubuntu16.04</code>. This works well, but requires to build two images, which takes twice the time and twice the disk space.  <em>Breaks requirement 1.</em></li>
  <li>Build the "normal" (mesa/intel) image first from <code>ubuntu:16.04</code>, do all the time-consuming stuff there, then use this as the base for another image where NVIDIA drivers are installed manually from the official "run file". This works if the driver matches exactly the driver installed on the host, but not if the host has a different (e.g. older) version. <em>Breaks requirement 2.</em></li>
  <li>Do nothing, just run my regular mesa-enabled container with <code>--runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=all -e NVIDIA_DRIVER_CAPABILITIES=all</code>. If I do, <code>nvidia-smi</code> sees the card, but OpenGL (e.g. <code>glxinfo</code>) still tries to load the <code>swrast</code> driver, which doesn't work.</li>
  </ul>
  
  <p>Most examples I've seen in the wild use the <code>nvidia/opengl:1.0-glvnd-runtime-ubuntu16.04</code> base, and for the life of me I cannot find how the nvidia drivers are installed (if at all) in that image. I've also read somewhere that with the nvidia container runtime (i.e. <code>nvidia-docker2</code>, which I'm using) you don't need to install the drivers, but that doesn't seem to be the case, at least not for OpenGL.</p>
  
  <p>So again, is there a way to create container images for nvidia and non-nvidia that satisfy all my requirements, or do I just want too much?</p>