Skip to content

Commit

Permalink
Add nvidia support
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensa committed Nov 14, 2019
1 parent b2753f9 commit f4c1609
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
14 changes: 13 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
FROM rubensa/ubuntu-tini-dev
LABEL author="Ruben Suarez <[email protected]>"

# If defined, install specified NVIDIA driver version
ARG NVIDIA_VERSION

# Tell docker that all future commands should be run as root
USER root

Expand All @@ -14,14 +17,23 @@ ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
#
# Install software and needed libraries
&& apt-get -y install module-init-tools libglib2.0-bin pulseaudio-utils cups-client x11-utils \
&& apt-get -y install module-init-tools libglib2.0-bin pulseaudio-utils cups-client x11-utils mesa-utils mesa-utils-extra va-driver-all \
#
# Assign audio group to non-root user
&& usermod -a -G audio ${USER_NAME} \
#
# Assign video group to non-root user
&& usermod -a -G video ${USER_NAME} \
#
# Install NVIDIA drivers
&& if [ ! -z ${NVIDIA_VERSION} ] ; \
then \
curl -O http://us.download.nvidia.com/XFree86/Linux-x86_64/${NVIDIA_VERSION}/NVIDIA-Linux-x86_64-${NVIDIA_VERSION}.run; \
chmod +x NVIDIA-Linux-x86_64-${NVIDIA_VERSION}.run; \
./NVIDIA-Linux-x86_64-${NVIDIA_VERSION}.run --ui=none --no-kernel-module --no-install-compat32-libs --install-libglvnd --no-questions; \
rm NVIDIA-Linux-x86_64-${NVIDIA_VERSION}.run; \
fi \
#
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
Expand Down
55 changes: 36 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ docker build --no-cache \
.
```

To make an Nvidia GPU available in the docker container, the following steps have to be taken:

```
#!/usr/bin/env bash
# NVidia propietary drivers are needed on host for this to work
NVIDIA_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader)
prepare_docker_nvidia_drivers_version() {
# On build, if you specify NVIDIA_VERSION the nvidia specified drivers version are installed
BUILD_ARGS+=" --build-arg NVIDIA_VERSION=$NVIDIA_VERSION"
}
prepare_docker_nvidia_drivers_version
docker build --no-cache \
-t "rubensa/ubuntu-tini-x11" \
--label "maintainer=Ruben Suarez <[email protected]>" \
${BUILD_ARGS} \
.
```

## Running

You can run the container like this (change --rm with -d if you don't want the container to be removed on stop):
Expand Down Expand Up @@ -80,6 +102,15 @@ prepare_docker_webcam_host_sharing() {
prepare_docker_gpu_host_sharing() {
# GPU support (Direct Rendering Manager)
[ -d /dev/dri ] && DEVICES+=" --device /dev/dri"
# VGA Arbiter
[ -c /dev/vga_arbiter ] && DEVICES+=" --device /dev/vga_arbiter"
# Allow nvidia devices access
for device in /dev/nvidia*
do
if [[ -c $device ]]; then
DEVICES+=" --device $device"
fi
done
}
prepare_docker_printer_host_sharing() {
Expand Down Expand Up @@ -131,25 +162,6 @@ This way, the internal user UID an group GID are changed to the current host use

Functions prepare_docker_dbus_host_sharing, prepare_docker_xdg_runtime_dir_host_sharing, prepare_docker_sound_host_sharing, prepare_docker_webcam_host_sharing, prepare_docker_gpu_host_sharing, prepare_docker_printer_host_sharing, prepare_docker_x11_host_sharing and prepare_docker_hostname_host_sharing allows sharing your host resources with the running container as GUI apps can interact with your host system as they where installed in the host.

## Setup GPU

To make an Nvidia GPU available in the docker container, the following steps have to be taken:

On the host, check your driver version:

# you might have to install this package:
$ sudo apt-get install mesa-utils
$ glxinfo |grep "OpenGL version"
OpenGL version string: 4.6.0 NVIDIA 390.129

In this example, the driver version is "390.129". Now, you have install exactly the same driver in the container:

export VERSION=390.129
wget http://us.download.nvidia.com/XFree86/Linux-x86_64/$VERSION/NVIDIA-Linux-x86_64-$VERSION.run
chmod +x NVIDIA-Linux-x86_64-$VERSION.run
apt-get install -y module-init-tools
./NVIDIA-Linux-x86_64-$VERSION.run -a -N --ui=none --no-kernel-module

## Connect

You can connect to the running container like this:
Expand Down Expand Up @@ -203,6 +215,11 @@ You can check X11 running command:
xmessage 'Hello, World!'
```

Your can chedk GPU acceleration is working running command:
```
glxgears
```

## Stop

You can stop the running container like this:
Expand Down
17 changes: 17 additions & 0 deletions build-custom.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

# NVidia propietary drivers are needed on host for this to work
NVIDIA_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader)

prepare_docker_nvidia_drivers_version() {
# On build, if you specify NVIDIA_VERSION the nvidia specified drivers version are installed
BUILD_ARGS+=" --build-arg NVIDIA_VERSION=$NVIDIA_VERSION"
}

prepare_docker_nvidia_drivers_version

docker build --no-cache \
-t "rubensa/ubuntu-tini-x11" \
--label "maintainer=Ruben Suarez <[email protected]>" \
${BUILD_ARGS} \
.
9 changes: 9 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ prepare_docker_webcam_host_sharing() {
prepare_docker_gpu_host_sharing() {
# GPU support (Direct Rendering Manager)
[ -d /dev/dri ] && DEVICES+=" --device /dev/dri"
# VGA Arbiter
[ -c /dev/vga_arbiter ] && DEVICES+=" --device /dev/vga_arbiter"
# Allow nvidia devices access
for device in /dev/nvidia*
do
if [[ -c $device ]]; then
DEVICES+=" --device $device"
fi
done
}

prepare_docker_printer_host_sharing() {
Expand Down

0 comments on commit f4c1609

Please sign in to comment.