cudadocker

Using GPU from a docker container?


I'm searching for a way to use the GPU from inside a docker container.

The container will execute arbitrary code so i don't want to use the privileged mode.

Any tips?

From previous research i understood that run -v and/or LXC cgroup was the way to go but i'm not sure how to pull that off exactly


Solution

  • Writing an updated answer since most of the already present answers are obsolete as of now.

    Versions earlier than Docker 19.03 used to require nvidia-docker2 and the --runtime=nvidia flag.

    Since Docker 19.03, you need to install nvidia-container-toolkit package and then use the --gpus all flag.

    So, here are the basics,

    Package Installation

    Install the nvidia-container-toolkit package as per official documentation at Github.

    For Redhat based OSes, execute the following set of commands:

    $ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
    $ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.repo | sudo tee /etc/yum.repos.d/nvidia-docker.repo
    
    $ sudo yum install -y nvidia-container-toolkit
    $ sudo systemctl restart docker
    

    For Debian based OSes, execute the following set of commands:

    # Add the package repositories
    $ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
    $ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
    $ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
    
    $ sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
    $ sudo systemctl restart docker
    

    Running the docker with GPU support

    docker run --name my_all_gpu_container --gpus all -t nvidia/cuda
    

    Please note, the flag --gpus all is used to assign all available gpus to the docker container.

    To assign specific gpu to the docker container (in case of multiple GPUs available in your machine)

    docker run --name my_first_gpu_container --gpus device=0 nvidia/cuda
    

    Or

    docker run --name my_first_gpu_container --gpus '"device=0"' nvidia/cuda