Howto get hardware accelerated OpenGL support in Docker
I recently played around with Docker and tried to get OpenGL with hardware acceleration/direct rendering (DRI) to work. This was no big deal, but there are some things that must be considered and I want to share with you:
- The graphics driver in the docker container must be the same as on the host system.
- The X server on the host must permit connections from the docker container (this is what
xhost +
in run.sh does). - Note that
xhost +
is security wise not ideal as it allows connections the the X server from any host (I have not investigated in a better solution) - The container must be started with the arguments
-e "DISPLAY=unix:0.0" -v="/tmp/.X11-unix:/tmp/.X11-unix:rw" --privileged
- this enables the docker container to use the X server of your host.--privileged
is essential for DRI support.
I’ve pushed Dockerfiles and corresponding build- and run-scripts that take care of the points mentioned above to the github repository docker3d.
Build the image
To build the image, first clone the docker3d repository.
if your host has an nvidia card and uses the official nvidia driver
- Download the nvidia driver for Linux from the nvidia homepage (take care it is the same version as installed on your host) and place it into the directory
ubuntuWithNvidiaDriver
. - Change to the directory
ubuntuWithNvidiaDriver
. - Execute
build.sh
- this builds the ubuntu based image and installs the nvidia driver.
if your host uses an open source driver
- Change to the directory
ubuntuWithOpenSourceDrivers
. - Execute
build.sh
to build the ubuntu based image and install (all!) available opensource drivers.
if your host has an ATI card and uses the official catalyst driver
The procedure should be similar to the “nvidia” procedure but I’ve not tested it.
Create a container and test it
- Execute
./run.sh
in the corresponding subdirectory to get a bash prompt in the created docker container. - To see if you have hardware acceleration, call
glxinfo
. You should seedirect rendering: Yes
- Call
glxgears
to verify it’s working.