3. Use g-learn Docker Container on GPU#

This method neither requires installing CUDA nor g-learn as all are pre-installed in a docker image.

3.1. Install Docker#

First, install docker. Briefly:

sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
    sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
    https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
sudo systemctl start docker
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
sudo systemctl start docker

Configure docker to run docker without sudo password by

sudo groupadd docker
sudo usermod -aG docker $USER

Then, log out and log back. If docker is installed on a virtual machine, restart the virtual machine for changes to take effect.

3.2. Install NVIDIA Container Toolkit#

To access host’s GPU device from a docker container, install NVIDIA Container Toolkit as follows.

Add the package to the repository:

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 yum-config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Install nvidia-contaner-toolkit by:

sudo apt update
sudo apt install -y nvidia-container-toolkit
sudo yum install -y https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el7.x86_64.rpm
sudo dnf install -y https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el7.x86_64.rpm

Restart docker:

sudo systemctl restart docker

3.3. Get g-learn Docker image#

docker-size

Get the g-learn docker image by

docker pull sameli/glearn

The docker image has the followings pre-installed:

  • CUDA: in /usr/local/cuda

  • Python 3.9: in /usr/bin/python3

  • Python interpreters: ipython, jupyter

  • Editor: vim

3.4. Use g-learn Docker Container on GPU#

To use host’s GPU from the docker container, add --gpus all to any of the docker run commands, such as by

docker run --gpus all -it sameli/glearn

The followings are some examples of using docker run with various options:

  • To check the host’s NVIDIA driver version, CUDA runtime library version, and list of available GPU devices, run nvida-smi command by:

    docker run --gpus all sameli/glearn nvidia-smi
    
  • To run the container and open Python interpreter directly at startup:

    docker run -it --gpus all sameli/glearn
    

    This also imports g-learn package automatically.

  • To run the container and open IPython interpreter directly at startup:

    docker run -it --gpus all sameli/glearn ipython
    

    This also imports glearn package automatically.

  • To open Bash shell only:

    docker run -it --gpus all --entrypoint /bin/bash sameli/glearn
    
  • To mount a host’s directory, such as /home/user/project, onto a directory of the docker’s container, such as /root, use:

    docker run -it --gpus all -v /home/user/project:/root sameli/glearn