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:
What I've tried so far:
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.
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.
--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?
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.
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.
The official PyTorch Docker image is based on nvidia/cuda , which is able to run on Docker CE, without any 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With