PyTorch With Docker

Zaher Abd Ulmaula
4 min readJul 5, 2019

--

Setup machine with different PyTorch versions to run on Nivida GPU is not a simple task, but using Docker containers makes it easier and productive. If you are interested in deep learning, you might hear about PyTorch and Tensorflow, which are frameworks to build Deep learning models. Both of them are compatible to run on Nvidia GPUs by using The CUDA library to use GPUs power in train deep models faster. Docker makes installing all that libraries more comfortable and isolates them from machine to use different versions at the same time without any problem.

Installing PyTorch or Tensorflow with CUDA is not always a tricky job because PyTorch and Tensorflow work with specific versions of CUDA. For example, you can install PyTorch 1.1.0 with CUDA 9.0 or CUDA 10.0. Code written by PyTorch 1.1.0 does not run on earlier versions of CUDA like 8.0. Tensorflow has the same issue because of the system relay on hardware drivers at the end. Also, PyTorch and Tensorflow have another problem, they get an update from time to time, and not all versions are running the older code. So, if you have a pre-trained model is hard to run it on recent releases. Even you need to modify the code which affects running the pre-trained model. Alternatively, deal with CUDA configuration to run different CUDAs versions at the same machine, which is a hard task.

The best solution I found is using Docker to use containers for running different versions of libraries at the same time. It makes without changing the configuration of the machine. In this article, I am explaining my method of how you could use Docker to install any version PyTorch and CUDA at the same time without conflict. Still, there is one limitation, which is the GPU. Each Nvidia GPU works with limited releases of CUDA. I have Geforce-1080, and it works with CUDA 8 until the latest version 10.1.

Docker

Docker which is a tool to create and run a Linux container. It’s an application that works like a virtual machine but faster and more powerful because it works with the operating system of the device. I am still learning about Docker, but it is effortless to use. For the propose of this article, you don’t have to be an expert in Docker.

The Docker documentation page explains all the details you need to install Docker on most operating systems. It is a straightforward task if you follow it step by step.

NVIDIA Container Runtime for Docker

Nvidia runtime container is mandatory to run PyTorch with GPU. Installing it is not hard just a few commands. You can check this Github repository for more information installation instructions.

PyTorch Image

In Github, there are some repositories created by good developers. PyTorch Docker image is repository has many PyTorch images for most of the previous versions with compatible CUDA versions. I tried most of those images to run the code I have. To install the image, I used the “pull command.”

# docker pull image_name:image_tag
$ docker pull anibali/pytorch:cuda-10.0

In the table, we can find the image tag, which is the part after the : .

https://github.com/anibali/docker-pytorch

Using Image

According to the repository documentation, there are some flags need to declare to run the Nvidia runtime container with PyTorch container.

$ docker run --rm -it --init \
--runtime=nvidia \
--ipc=host \
--user="$(id -u):$(id -g)" \
--volume="$PWD:/app" \
-e NVIDIA_VISIBLE_DEVICES=0 \
anibali/pytorch python3 main.py

The only issue you may find problematic is reading dataset. In my case, I am using “Dataloader,” and the dataset was out of a model directory; so the container could not read the dataset. The easy solution is to move the dataset and put it inside the main directory. Another solution is to make — -volume it is associated with the directory containing the script folder and dataset.

Python Dependencies

By default, there are many dependencies installed with the PyTorch Image. You can check the list here. Some project needs more than that. I found the only way is to build the Docker file on your machine after adding this command for each package inside docker file:

RUN pip install package-name

However, I found that an annoying way to add packages, you need to modify docker file each time you want to use it for a new project. So, after searching, I found you could use “requirements.txt” file, which helps to keep docker file clean, and you can add the name of dependencies in “requirements.txt” file. For example:

scikit-image==0.15

Even an empty file “requirements.txt” not affect building the image.

Therefore, I modified the repository for my Github Fork. You can use it, and I put the instructions to build and run your custom image.

Those are all the information you need to run and manage PyTorch containers. Docker is a straightforward tool and helps to isolate the machine configuration from the code you try to run.

Finally, thanks for reading and this my first article, please, write a comment to help me improve my future ones.

--

--