How To Use a GPU With Unraid And Docker

Containers, Docker, GPU, Unraid -

How To Use a GPU With Unraid And Docker

If you are running Unraid, I think there’s a fair chance you may be storing some media files (or “Linux ISOs” as commonly called by the Homelab community) on your server that could be transcoded or converted into different encodings. This could be done by dedicated CLI tools or apps or, more commonly, by applications that may convert these in real-time, like media server apps Plex or JellyFin. These tools may be able to use a spare GPU you have lying around to allow you to use that GPU compute to assist in the media encoding and decoding process, speed up the process and possibly free up compute on your CPU as that work is offloaded!

Out of the box, Unraid does not support the installation and passthrough of a GPU, but with some easy plugin installation and configuration, we can use it in our docker containers to utilise in the applications you may be running in your Unraid instance. In this post, we will cover this installation and configuration process for Nvidia GPUs and show how you can use it with Plex to speed up the media conversion process.

Installing A GPU Driver On Unraid

Once your GPU is physically installed in your Unraid machine, you are ready to configure your docker applications to consume this resource via the unique UUID of the resources. Installing the GPU drivers is the first step to getting the GPU to function on your Unraid box. Via the community plugin application, you can use the Nvidia-Driver. If you have installed a plugin before, this should be easy. You can find the detailed instructions for installing this below:

Once the plugin has been installed, wait for all the processes to be completed and reboot the system. You are ready to use the plugin. You can access this under the settings tab of Unraid, and you’ll be treated to a screen like the one below.

From this page, you must select the driver versions for your graphics card. You can find the latest package version on the Nvidia website, but these versions are not for the Linux Nvidia package. Once your Nvidia package is installed, you will want to take note of your GPU UUID. This is the value starting with GPU-*.

 

Do note that if you select the wrong Nvidia GPU driver during the process and install it, your Unraid instance will boot, but any docker container that attempts to utilise the GPU with the wrong drive will fail to start. Return to the Unraid settings screen and select another driver if this happens. Start with the latest production version and then work your way down the available versions until you find one that works for your GPU. Generally, the older the card, the older the driver it will be able to run.

Using Your GPU with Docker Containers

Now that your GPU is installed and the drives are present, you’ll need to update Docker to use the GPU. We will first need to update the Docker engine to use the Nvidia runtime and make this available for your containers. You must add the following config to your docker daemon.json file at /etc/docker/daemon.json. You can find a snippet of the required addition to the runtimes property below:

```
{
  "registry-mirrors": [],
  "insecure-registries": [],
  "runtimes": {
    "nvidia": {
      "path": "/usr/bin/nvidia-container-runtime",
      "runtimeArgs": []
    }
  }
}

```

You should be ready to use a container once you have added it and restarted Docker or the whole Unraid system. With the runtime available in the Docker engine and the ability to consume the container, you will want to add the following configuration to your container definitions. Below, you can see the container added to the Plex container for upscaling or downscaling content.

 

```
version: "2.3"

services:
  plex:
    image: "linuxserver/plex"
    container_name: "plex"
    volumes:
    - "/mnt/media/tv:/media/tv"
      - "/mnt/media/movies:/media/movies"
      - "/opt/plex:/config"
      - "/etc/localtime:/etc/localtime:ro"
    network_mode: host
    restart: unless-stopped
    # Enable GPU
    runtime: nvidia
    environment:
      - "NVIDIA_DRIVER_CAPABILITIES=all"
      - "NVIDIA_VISIBLE_DEVICES=<UUID COLLECTED EARLIER>"

```

The important property for this configuration is the runtime property that points to the Nvidia runtime you configured for the docker daemon. You will also need to set the following 2 environment variables for your container:

  • NVIDIA_DRIVER_CAPABILITIES: This tells the Nvidia driver inside the container to activate the driver’s available capabilities.
  • NVIDIA_VISIBLE_DEVICES: The following property will refer to the ID you gathered from the `Nvidia-Driver` plugin setting page to tell the driver which GPU the container can use. You can use the same GPU across multiple containers on the docker host.

Note that utilisation depends on the application’s ability to interact with the GPU driver and hardware. Unfortunately, you don’t get increased performance via your GPU just by adding it to a container. Now, once you deploy your container, it can see and use the GPU for its processing to speed it up and save some of those CPU cycles for other processes.

Wrapping Up

You should now have a better idea of how to use some of that old hardware you might have lying around, like a GPU from your old gaming PC, to make some of the apps on your Unraid machine faster and free up some performance on your system’s CPU. With some simple configuration, you can be up and running quickly today.



Leave a comment