Fix NVIDIA WSL2 container issues - Part II (proper way)

Proper fix for WSL + docker + NVIDIA issue

In my previous blog, I wrote about fixing docker issues when using NVIDIA/CUDA enabled docker containers.
For some of my containers it worked, but for others it didn't. I had problem with one of my containers that uses torch (C++). The 'nvidia-smi' command worked fine inside the container, showing my GPU as expected. But, torch wasn't able to detect any device. So, I was back to square one. Below you will the steps to make the container working properly (also without the need to strip files from the original image).

In order to setup things properly.

Now you can import the docker container in your WSL image

#show available distros wsl --list Windows Subsystem for Linux Distributions: YOUR_DISTRO #Start your distro in WSL2. From command prompt: wsl -u USERNAME -d YOUR_DISTRO #within the WSL2 distro, import the exported container: #docker import FILE_NAME TAG docker import /mnt/c/foo.tar YOUR_DISTRO:1.0 #after import your image should be available. You can checks this via: docker image list

Now you can run the container with GPU support

#docker run --rm --gpus all -p OUTSIDE_PORT:INSIDE_PORT --name YOUR_CONTAINER_NAME IMAGE_REPO:VERSION STARTUP_COMMAND_INSIDE_CONTAINER --runtime=nvidia docker run --rm --gpus all -p 5000:5000 --name YOUR_CONTAINER_NAME YOUR_DISTRO:1.0 /home/johndoe/foo.bar --runtime=nvidia

Side note 1: docker.io vs docker-ce

When installing docker inside your WSL image, you have two options: docker.io (provided/maintained by Debian/Ubunut) and docker-ce (provided and maintained by docker.com). In my previous WSL images I used docker.io as it seemed more close to Ubuntu/Debian, but I found out that it is better to use docker-ce (at least at the time of this writing).

Side note 2: WSL2 + NVIDIA vs WSL2 + NVIDIA Container Toolkit

There are two options to get NVIDIA support inside your WSL2 image as described by: The first one is for compiling/running CUDA enabled code inside your WSL2 image. The latter is for running NVIDIA enabled containers inside your WSL2 image. These are not the same and might be confusing. So be sure to follow the right guide.

Back to List

All form fields are required.
A confirmation mail for the comments will be send to you.