Skip to content

NVIDIA driver support for docker container

mviereck edited this page Dec 10, 2019 · 57 revisions

NVIDIA driver support by x11docker

Proprietary closed source drivers from NVIDIA corporation need some manual setup and have some constraints. Consider to use free nouveau driver instead.

The container needs the very same NVIDIA driver version as the host. Basically there are three ways that make sense:

  • Providing an NVIDIA installer file for automated installation in container by x11docker. Useful e.g. for developers who want to deploy images with OpenGL applications.
    • Advantage: This is flexible and portable and does not need specific image setups.
    • Drawback: Automated install of NVIDIA driver slows down every container startup a bit.
  • Creating a base image with an NVIDIA driver that matches the NVIDIA driver on host. Other images base upon that.
    • Advantage: Best performance and disk space usage for end users who want to set up several containerized applications for own use.
    • Drawbacks: Every update of the NVIDIA driver on host needs a rebuild of all images. The base image matches a few host systems only and is not portable.
  • NVIDIA/nvidia-docker images somehow use driver files from host.
    • Advantage: No need for a driver installation in image or container.
    • Drawbacks: Needs configuration changes of systemd and docker daemon on host. Bound to provided base images. Images do not run on systems with open source drivers.

For usage of NVIDIA GPU without x11docker look at wiki: How to set up GPU hardware acceleration for docker containers.

Automated install of NVIDIA driver during container startup

x11docker can automatically install closed source NVIDIA drivers in container at every container startup. It gives some setup instructions and a probably matching download link in terminal output.

  • x11docker needs an installer file that matches the version on host. It must not be a deb or rpm package but an NVIDIA_[...].run file. Store it at one of the following locations:
    • ~/.local/share/x11docker (current user only)
    • /usr/local/share/x11docker (system wide)
  • Find out installed NVIDIA driver version on host with:
    • head -n1 </proc/driver/nvidia/version | awk '{ print $8 }'
  • Look at NVIDIA driver download page or try the direct download link provided in x11docker terminal output.

NVIDIA driver base image

Script to build a base image with NVIDIA driver matching the version on host. (Not needed if using automated installation as described above).

#! /bin/bash

# Script to build image x11docker/nvidia-base
# containing NVIDIA driver version matching the one on host.

Imagename="x11docker/nvidia-base"

Nvidiaversion="$(head -n1 </proc/driver/nvidia/version | awk '{ print $8 }')"
[ "$Nvidiaversion" ] || {
  echo "Error: No NVIDIA driver detected on host" >&2
  exit 1
}
echo "Detected NVIDIA driver version: $Nvidiaversion"

Driverurl="https://http.download.nvidia.com/XFree86/Linux-x86_64/$Nvidiaversion/NVIDIA-Linux-x86_64-$Nvidiaversion.run"
echo "Driver download URL: $Driverurl"

Tmpdir="/tmp/x11docker-nvidia-base"
mkdir -p "$Tmpdir"

echo "# Dockerfile to create NVIDIA driver base image $Imagename
FROM debian:stable
RUN apt-get update && \
    apt-get install --no-install-recommends -y kmod xz-utils wget ca-certificates binutils || exit 1 ; \
    wget $Driverurl -O /tmp/NVIDIA-installer.run || exit 1 ; \
    Nvidiaoptions='--accept-license --no-runlevel-check --no-questions --no-backup --ui=none --no-kernel-module --no-nouveau-check' ; \
    sh /tmp/NVIDIA-installer.run -A | grep -q -- '--install-libglvnd'        && Nvidiaoptions=\"\$Nvidiaoptions --install-libglvnd\" ; \
    sh /tmp/NVIDIA-installer.run -A | grep -q -- '--no-nvidia-modprobe'      && Nvidiaoptions=\"\$Nvidiaoptions --no-nvidia-modprobe\" ; \
    sh /tmp/NVIDIA-installer.run -A | grep -q -- '--no-kernel-module-source' && Nvidiaoptions=\"\$Nvidiaoptions --no-kernel-module-source\" ; \
    sh /tmp/NVIDIA-installer.run \$Nvidiaoptions || { echo 'ERROR: Installation of NVIDIA driver failed.' >&2 ; exit 1 ; } ; \
    rm /tmp/NVIDIA-installer.run ; \
    apt-get remove -y kmod xz-utils wget ca-certificates binutils ; \
    apt-get autoremove -y ; apt-get clean -y
" >"$Tmpdir/Dockerfile"

echo "Creating docker image $Imagename"
docker build -t $Imagename $Tmpdir || {
  echo "Error: Failed to build image $Imagename.
  Check possible build error messages above.
  Make sure that you have permission to start docker.
  Make sure docker daemon is running." >&2
  rm -R "$Tmpdir"
  exit 1
}

echo "Successfully created $Imagename"
rm -R "$Tmpdir"
exit 0

Create your own images basing upon this new image x11docker/nvidia-base. Example:

FROM x11docker/nvidia-base
RUN apt-get update && apt-get install -y xterm
CMD xterm

NVIDIA/nvidia-docker images

If your host system is configured for NVIDIA/nvidia-docker, you can use the according images with x11docker option --runtime=nvidia. Compare README.md: Container runtime. Example:

x11docker --gpu --runtime=nvidia -- nvidia/cuda nvidia-smi

Be aware that these images do not support systems with open source drivers.

Constraints

  • NVIDIA does not support Wayland and Xwayland that are needed for some more advanced x11docker X server options. Hardware acceleration (option --gpu) with proprietary NVIDIA drivers will only work for insecure option --hostdisplay and for option --xorg.
  • Closed source NVIDIA driver installation fails in container systems that are not based on glibc. This affects especially Alpine based images. NVIDIA corporation does not provide the source code that would allow you to use your hardware with different systems.
  • Free nouveau driver for NVIDIA cards works quite well with x11docker and does not need a special setup. However, NVIDIA corporation does not even publish the API of your NVIDIA card. The developers of nouveau are forced to spend their power in a lot of reverse engineering. Due to this secretive policy of NVIDIA corporation the free and open source nouveau driver cannot support all features of NVIDIA cards.
  • To be honest.
Clone this wiki locally