Sharing a GPU with multiple containers

Containers will share a GPU.

On a laptop, you probably have two GPUs. I would share only one with your containers. First figure out what PCI address it has:

incus info --resources | grep "GPU:" -A 20

Then pass a GPU to the container like this:

incus config device add <container_name> gpu gpu gid=44 pci="0000:05:00.0"

Where 44 is the GID of video group inside your container. You can check it like this:

incus exec <container_name> -- sudo --login --user ubuntu bash -c "getent group video"

When running Plex, you probably want a hardware acceleration. For that, inside the container add the default user to the video and renderer groups (default user for ubuntu containers is ubuntu):

incus exec <container_name> -- sudo --login --user ubuntu
sudo usermod -a -G render,video $LOGNAME

Now check the /dev/dri/ folder inside the container:

ls -al /dev/dri/

It should look something like this:

crw-rw---- 1 root video 226,   0 Nov 15 08:41 card0
crw-rw---- 1 root video 226, 128 Nov 15 08:41 renderD128

Finally, restart the container.

1 Like