Dockerize your Tensorflow Development Environment

Source: Deep Learning on Medium

How to, Two Options Available!

Here we provide two options to achieve it:

  • We’ve published the image in Docker Hub, so if you are used to Docker you can start working with it directly! Here you have the repository, and you should look for Tensorflow on GPU since it will contain more environments on a near future.

Specific values of the Image are:

username: user
password: docker
sudo: yes
user id: 1000

If you.. :

  • Are not used to Docker or want a little help (volumes, …)
  • Want some config files into your own image ( .bashrc , …)
  • Want to change the user or user id to avoid permissions problems ( having the same user within the contanier ).

Then we have the second option:

  • We provide a script for it ( check tf_on_gpu folder in this repo ).

It can also copy files from a Config folder to customize your experience. These files are copied to both your user and root in the container, and include:

./Config/.bashrc # ~/.bashrc from host
./Config/.bash_aliases # ~/.bash_aliases from host
./Config/hosts # /etc/hosts from host
./Config/config # ~/.ssh/config file from host

As you may have noticed, they should be placed into the Config folder next to the Dockerfile so Docker build command finds them and adds them to the image.

Aside from copying files and installing packages, we take into account one important thing: permissions. We will pass our username and host username id to the container. This way it will match the host ID and therefore we will have the same permissions when working within the shared directories, avoiding some mess.

If you’re eager to just run your new environment within Docker, please jump over the next section. If you want a small insight on how does it work and what does it include, I encourage you to read this brief summary.

Docker Image — What does it include

Our dear NVIDIA’s friends have published an image based on ubuntu 18.04 , plus CUDA already installed. We have taken it as base image and added:

  1. cuDNN
  2. Python 3.6 ( also linked directly as python )
  3. CUDA related packages ( cublas , cusolver , …)
  4. Python related packages ( pip , setuptools , …)
  5. Common Linux tools ( wget , vim , git , curl , tree …)
  6. Python frameworks or packages ( OpenCV (cv2) , numpy , Sklearn , Pandas , matplotlib , scikit-learn , lxml , …) ( You can check here the full list )
  7. Tensorflow ( version selected on image build process)
  8. Keras ( if Tensorflow version lower than 2.0 )

The idea is basically to have almost everything that you may need already available on the image.

Execute it! — A new development environment

As said at the beginning you could do it by two different ways. Let’s see them both! Please notice that you need to have your GPU drivers installed on host. It’s the only step needed beforehand.

Now go to your bash terminal and just refer it by its name, the most basic example would be the following one (adding access to all GPU devices):

docker run -dit --gpus device=all naipsas/develop:tf_1.13.2_keras_2.2.5# Now go into the container to work
docker exec -it container_id /bin/bash

Please refer to NVIDIA Docker Github repo if you need to learn about --gpus flag, and consider checking the docker run command from the following recipe if you need to add parameters like X Server connection, volume mount…

  • Using the script / recipe:

You can perform them by following this recipe, or by using this script that I provide for you all (bash script, for Linux).

The script advantages? Take a look to the Github Readme right here. Note that the script options are in the same order as this recipe.

Script menu: as explained in the repo Readme, it has some extra capabilities

Install docker and nvidia-container-toolkit.

sudo apt install docker-cedistribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L | sudo apt-key add -
curl -s -L$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt update && sudo apt install nvidia-container-toolkitsudo systemctl restart docker

2. Create our image ( dockerfile is published, just here). Notice that from here on, you need to replace some fields like image_name in the code (name it as you want).

USERID="$(id -u)"
docker build -t image_name --build-arg CUDA=$cuda --build-arg CUDNN=$cudnn --build-arg TF=$TF --build-arg KERAS=$Keras --build-arg USER=$USER --build-arg USERID=$USERID .

3. Run a container: mounting needed folders to achieve a nice communication with our host file system, and allowing it to access our GPUs.

# This will allow you to have GUI applications running in the container, i.e. plots in your python code!
X11="-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix"
# GPUs, Replace for "all", or an array like "0,1" if you have more than one GPU
GPUs="--gpus \"device=${GPUs}\""
# Mount your needed folders
mount="--mount src=/home/my_user/my_repos,target=/my_repos,type=bind"
docker run $X11 $GPUs $mount container_name -dit image_name

Ready! Now just go into the container and develop! Let’s check if we have access to our GPUs by executing nvidia-smi and that Tensorflow works as expected:

X11="-e DISPLAY=$DISPLAY"docker exec $X11 -it your_container_name /bin/bash
Nvidia-smi shows that the GPU is available inside the container
Tensorflow loads correctly and is able to use our GPU

So that’s it! Now you’re ready to work!

Finally, I would ask you to comment on any problem or improvement that occurs to you. Thanks in advance!

Disclaimer: this work has been performed on Kubuntu 18.04 and any specific path or command may be due to it. Please take into account and modify as you need for your Linux distro.