Using imate on Docker#

Why Using Docker#

imate’s docker image can be very useful if you want to deploy imate on GPU devices. The imate package which is installed via pip or conda, natively supports GPU devices. However, the version of CUDA Toolkit that imate was built with it, should match the version of CUDA Toolkit that you have on your machine. This might often be a problem, as your CUDA Toolkit might not have the same version as the one that imate supports.

Workarounds to this problem are either:

Alternatively, as the third option, you can simply use imate’s docker image without installing imate or a compatible CUDA Toolkit as both are installed in the docker container and ready to use out of the box.

Install Docker#

The followings are instructions to install docker on Linux. To install on other operating systems, see Install Docker Engine from Docker documentation.

Install docker by

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.

Get imate Docker Image#

docker-size

Get the imate docker image by

docker pull sameli/imate

The docker image has the following pre-installed:

  • CUDA: in /usr/local/cuda

  • Python 3.9: in /usr/bin/python3

  • Python interpreters: ipython, jupyter

  • Editor: vim

Examples of Using imate Docker Container#

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

    docker run -it sameli/imate
    

    This also imports imate package automatically.

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

    docker run -it sameli/imate ipython
    

    This also imports imate package automatically.

  • To open Bash shell only:

    docker run -it --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 -v /home/user/project:/root sameli/imate
    

Deploy imate Docker Container on GPU#

To access the host’s GPU device from inside the docker container, you should install NVIDIA Container Toolkit.

Install NVIDIA Container Toolkit#

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

Run imate Docker Container on GPU#

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

docker run --gpus all -it sameli/imate