Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OpenGL inside docker with nvidia-docker2

I'm trying to run OpenGL applications (Gazebo) 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.

My requirements:

  1. 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.
  2. The nvidia container should work on different systems which may have different nvidia cards and driver versions installed.
  3. 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.

What I've tried so far:

  • Just build separate images for nvidia and everything else, with FROM nvidia/opengl:1.0-glvnd-runtime-ubuntu16.04. This works well, but requires to build two images, which takes twice the time and twice the disk space. Breaks requirement 1.
  • Build the "normal" (mesa/intel) image first from ubuntu:16.04, 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. Breaks requirement 2.
  • Do nothing, just run my regular mesa-enabled container with --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=all -e NVIDIA_DRIVER_CAPABILITIES=all. If I do, nvidia-smi sees the card, but OpenGL (e.g. glxinfo) still tries to load the swrast driver, which doesn't work.

Most examples I've seen in the wild use the nvidia/opengl:1.0-glvnd-runtime-ubuntu16.04 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. nvidia-docker2, 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.

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?

like image 226
Daniele Avatar asked Dec 15 '18 04:12

Daniele


People also ask

Is nvidia docker2 deprecated?

With the release of Docker 19.03, usage of nvidia-docker2 packages is deprecated since NVIDIA GPUs are now natively supported as devices in the Docker runtime.

How do I use nvidia GPU with Docker containers?

To use your GPU with Docker, begin by adding the NVIDIA Container Toolkit to your host. This integrates into Docker Engine to automatically configure your containers for GPU support. The Container Toolkit should now be operational. You're ready to start a test container.

Can I use nvidia-Docker without nvidia GPU?

The official PyTorch Docker image is based on nvidia/cuda , which is able to run on Docker CE, without any GPU.

Can Docker containers share GPU?

Build and run Docker containers leveraging NVIDIA GPUsNVIDIA engineers found a way to share GPU drivers from host to containers, without having them installed on each container individually. GPUs on container would be the host container ones.


1 Answers

Why waste time trying to figure out a solution yourself when you can just "steal" someone else's? Especially if that someone else is NVIDIA themselves.

Since nvidia/opengl:1.0-glvnd-runtime-ubuntu16.04 seems to work well, but using it as a base breaks requirement 1, I can just copy files out of it and into my image instead.

Here ${from} points to my original, non-nvidia-aware container image (but I also tested it with from=ubuntu:16.04), and I just copy nvidia's drivers and configuration over:

ARG from
FROM nvidia/opengl:1.0-glvnd-runtime-ubuntu16.04 as nvidia
FROM ${from}

COPY --from=nvidia /usr/local /usr/local
COPY --from=nvidia /etc/ld.so.conf.d/glvnd.conf /etc/ld.so.conf.d/glvnd.conf

ENV NVIDIA_VISIBLE_DEVICES=all NVIDIA_DRIVER_CAPABILITIES=all

With this, and my ${from} built on top of ubuntu:16.04, glxinfo returns the expected configuration (NVIDIA being the GL vendor), and I can run Gazebo, Blender, etc. just like on the host. The beauty of this is, the resulting container works even when not using the nvidia runtime, on a system without nvidia drivers, it just gracefully falls back to using Mesa (I guess that's what "glvnd" does).

While I am currently required to use Ubuntu 16.04, I see no reason why a similar approach wouldn't work for other Ubuntu versions.

like image 138
Daniele Avatar answered Oct 05 '22 09:10

Daniele