Using g-learn on Docker#
Why Using Docker#
g-learn’s docker image can be very useful if you want to deploy g-learn on GPU devices. The g-learn package which is installed via pip
or conda
, natively supports GPU devices. However, the version of CUDA Toolkit that g-learn 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 g-learn supports.
Workarounds to this problem are either:
Change your CUDA installation. See Install CUDA Toolkit.
Compile
imate
(a package used by g-learn) with a specific CUDA version compatible with your existing CUDA installation. See Compile imate from Source.
Alternatively, as the third option, you can simply use g-learn’s docker image without installing g-learn 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 g-learn Docker Image#
Get the g-learn docker image by
docker pull sameli/glearn
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 g-learn 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/glearn nvidia-smi
To run the container and open Python interpreter directly at startup:
docker run -it sameli/glearn
This also imports g-learn package automatically.
To run the container and open IPython interpreter directly at startup:
docker run -it sameli/glearn ipython
This also imports glearn package automatically.
To open Bash shell only:
docker run -it --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 -v /home/user/project:/root sameli/glearn
Deploy g-learn 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 g-learn 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/glearn