Walkthrough: Working with Tensorflow on Windows through Docker


Versions of the dependencies in the tutorial

  • Python: 2.7
  • Tensorflow: 0.11.0rc2
  • Docker: 1.12.2
  • Windows: 8 (64-bit)

If you have looked at Tensorflow not much guidance exist for getting it to work in Windows.

This page includes installation instructions for Tensorflow (instructions). But not so much for Windows. In fact, the word “Windows” pretty much doesn’t exist on that page right now. One possible way to do is through Docker. Let’s get cracking.

Installing Docker

Installing docker is quite straight forward. After successful installation, you should get the following terminal when you click on Docker Quickstart Terminal.

Successful Terminal

Note: If you get a complaint about missing shortcut, connect the shortcut to something like C:\Program Files\Docker Toolbox\start.sh or Git.exe

Running Tensorflow on Docker

  1. Fire up the Docker terminal and type the following.
    docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow
    Delving into the details, -it is short for -i and -t. -i and -t allocates a terminal for the docker console. So you can use commands such as CTRL+c to terminate a program. (Run docker run --help for more information about various flags)
  2. Next, you can see if your Container is functioning. Whip out another docker terminal and type this docker ps -a. You should see the following.
    CONTAINER ID        IMAGE                          COMMAND             CREATED           STATUS              PORTS                              NAMES
    9bec23d5a820        gcr.io/tensorflow/tensorflow   "/run_jupyter.sh"   34 minutes ago      Up 34 minutes       6006/tcp, 0.0.0.0:8888->8888/tcp   nauseous_lichterman
    
  3. First, the docker image will be downloaded from gcr.io/tensorflow/tensorflow to the container
    Next, ipython_notebook.sh will be run enabling you to open an ipython notebook
  4. Open the ipython notebook dashboard on the browser by typing <ip>:8888 You can find your <ip> by typing docker-machine ip machine-name. You can find the machine-name by running docker-machine ls. You will something like following (My machine name is “default”)
    NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
    default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.3
    

You can stop here if you want to create a new script and fiddle around in that. However, note that you will lose your data once you kill or stop your container.

Making the host directory sharable

So you do not want to lose data, what should you do. Apparently the process is straight forward, but it is a bumpy ride to the end! So we’ll take it slow and get this right.

On a highlevel note, we will mount an existing folder on the host, so the data will be saved on the host, than being created virtually.

First make sure whatever the folder you plan to share is sharable[2] and listed as a shared folder in the VM [3] (Refer the references for exact how to do this).

Sharing folders on VM Settings

So you can see my C:\Users folder is shared and auto-mounts as well.

Resources for sharing drives on Docker
[1] https://github.com/docker/docker/issues/22981
[2] https://docs.docker.com/docker-for-windows/troubleshoot/
[3] https://blog.pavelsklenar.com/5-useful-docker-tip-and-tricks-on-windows/
[4] https://docs.docker.com/engine/tutorials/dockervolumes/

Mounting the host directory to Docker

Now we’re ready to complete the final step. Create the container with the correct host directory mounted to the container. To illustrate the example I will be using the data required for Assignment 2 from Udacity Deep Learning course. We are going to need to upload the 2_fullyconnected.ipynb file and notMNIST.pickle data file to the container.

run the following command
docker run -p 8888:8888 -v /c/Users/udacity_deeplearning:/notebooks -it --rm gcr.io/tensorflow/tensorflow

-p allows to publish on the given port, -v mounts a <source> folder to a <destination> folder (-v <source>:<destination>) -it explained earlier and gcr.io/tensorflow/tensorflow is the image

You should see the content in the udacity_deeplearning folder in the browser once you type <ip>:8888

Resources for the specific assignment
[5] https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/udacity

Bonus: Exploring the file structure within the container

The following command will take you to the root directory of the container.
docker exec -it /bin/bash

You can find the container ID by running
docker ps -a

Next you can use manual commands such as ls to look at the file structure in the Container

This is the Docker Cheat Sheet which will be a good reference point to understand how docker works

Bonus: Installing tensorflow for Python 3

So I couldn’t find any resources to do this with python 3 on the official tensorflow website. But it is possible to follow the same process with an docker image that supports python 3 and tensorflow. You can use the following command instead of the previous docker run command.
docker run -it -p 8888:8888 erroneousboat/tensorflow-python3-jupyter

Bonus: Other useful commands

If you are running out of memory from multiple images use the following commands to remove the unnecessary images
docker images will list the images you have in the computer
docker rmi <image_name> will remove the image you specify