3. Use imate Docker Container on GPU#

This method neither requires installing CUDA nor imate 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 imate Docker image#

docker-size

Get the imate docker image by

docker pull sameli/imate

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 imate 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/imate

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/imate nvidia-smi
    
  • To run the container and open Python interpreter directly at startup:

    docker run -it --gpus all sameli/imate
    

    This also imports imate package automatically.

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

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

    This also imports imate package automatically.

  • To open Bash shell only:

    docker run -it --gpus all --entrypoint /bin/bash sameli/imate
    
  • 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/imate