55

I've installed the tensorflow docker container on an ubuntu machine. The tensorflow docker setup instructions specify:

docker run -it b.gcr.io/tensorflow/tensorflow

This puts me into the docker container terminal, and I can run python and execute the Hello World example. I can also manually run .\run_jupyter.sh to start the jupyter notebook. However, I can't reach the notebook from host.

How do I start the jupyter notebook such that I can use the notebook from the host machine? Ideally I would like to use docker to launch the container and start jupyter in a single command.

1
  • 1
    The given command does not put me into a terminal (unless I append bash to the command-line). Instead it automatically runs /run_jupyter.sh. To get a separate terminal, you need to do something like docker exec -it [CONTAINER ID] bash, as described here. Commented May 15, 2016 at 18:45

10 Answers 10

49

For a Linux host Robert Graves answer will work, but for Mac OS X or Windows there is more to be done because docker runs in a virtual machine.

So to begin launch the docker shell (or any shell if you are using Linux) and run the following command to launch a new TensorFlow container:

docker run -p 8888:8888 -p 6006:6006 b.gcr.io/tensorflow/tensorflow ./run_jupyter.sh

Then for Mac OS X and Windows you need to do the following only once:

  1. Open VirtualBox
  2. Click on the docker vm (mine was automatically named "default")
  3. Open the settings by clicking settings
  4. In the network settings open the port forwarding dialog
  5. Click the + symbol to add another port and connect a port from your mac to the VM by filling in the dialog as shown below. In this example I chose port 8810 because I run other notebooks using port 8888. enter image description here
  6. then open a browser and connect to http://localhost:8810 (or whichever port you set in the host port section
  7. Make your fancy pants machine learning app!
9
  • 3
    Craig, thanks I got this running in Windows with your instructions. On Windows, I was not able to get it working within the Docker Quickstart Terminal. The terminal fails to handle forward slashes / correctly. Using PowerShell did work. I got to PowerShell with all the correct environment variables by launching Kinematic and then clicking "Docker CLI" in the bottom left corner of the app. Commented Nov 11, 2015 at 16:26
  • I'm glad you were interested in the non-linux cases as well. When I saw your answer I wasn't sure if I should make a new question or post it here. Thanks for getting it started!
    – Craig
    Commented Nov 11, 2015 at 17:49
  • 2
    When docker is installed on Windows (and probably OS X too), it defaults to 1 processor and 1GB RAM. If your computer can handle it, I suggest changing the VM settings to add more processors and memory. Commented Nov 11, 2015 at 18:23
  • Important: leave the guest IP blank. I filled it with 127.0.0.1 and it took me a while to realize that was the problem.
    – erickrf
    Commented Dec 2, 2015 at 21:50
  • 1
    @Robert Graves I did not succeed with the command line examples given, but Kitematic solved it all for me. I found a docker image that contained tensorflow and jupyter "tensorflow-jupyter". Just clicking on the "web preview" on the right side of the Kitematic window gave me a browser window (chrome) on Windows10 that is served by the Linux distro inside the docker container. Commented Feb 14, 2016 at 7:50
20

My simple yet efficient workflow:

TL;DR version:

  1. Open Docker Quickstart Terminal. If it is already open, run $ cd
  2. Run this once: $ docker run -it -p 8888:8888 -p 6006:6006 -v /$(pwd)/tensorflow:/notebooks --name tf b.gcr.io/tensorflow/tensorflow
  3. To start every time: $ docker start -i tf

If you are not on windows, you should probably change /$(pwd) to $(pwd)

You will get an empty folder named tensorflow in your home directory for use as a persistent storage of project files such as Ipython Notebooks and datasets.

Explanation:

  1. cd for making sure you are in your home directory.
  2. params:
    • -it stands for interactive, so you can interact with the container in the terminal environment.
    • -v host_folder:container_folder enables sharing a folder between the host and the container. The host folder should be inside your home directory. /$(pwd) translates to //c/Users/YOUR_USER_DIR in Windows 10. This folder is seen as notebooks directory in the container which is used by Ipython/Jupyter Notebook.
    • --name tf assigns the name tf to the container.
    • -p 8888:8888 -p 6006:6006 mapping ports of container to host, first pair for Jupyter notebook, the second one for Tensorboard
  3. -i stands for interactive

Running TensorFlow on the cloud

4
  • Thanks! Your solution is simple and works. A bit follow up question though, might seems silly cause I am new to docker, command line interface: Once I start the tf image, it automatically runs jupyter and the console is "occupied" by jupyter. I can't use it to execute other commands such as cd, ls...How to open a new console on the docker container? Commented Aug 4, 2016 at 3:43
  • -it in docker run -it ... stands for interactive. Try to remove it and I hope it will work as you intend.
    – Thoran
    Commented Aug 4, 2016 at 8:15
  • I tried but then then the Powershell remains connecting to windows only. Is there a way to connects to a container with two separate powershell? When one is "occupied" by Jupyter and becomes a display window for Jupyter messages, the other is still available for executing other commands on that container. I tried use the non-iteractive, but the powershell remains connected to windows, not the container. I then tried use "docker attach container_id", but then powershell console becomes no respondsive. Commented Aug 5, 2016 at 14:55
  • I think it's impossible since a container just run one application at a time. In my case, the jupyter notebook is running so there is no room for the same container to run a bash at the same time. Commented Aug 10, 2016 at 10:24
13

After further reading of docker documentation I have a solution that works for me:

docker run -p 8888:8888 -p 6006:6006 b.gcr.io/tensorflow/tensorflow ./run_jupyter.sh

The -p 8888:8888 and -p 6006:6006 expose the container ports to the host on the same port number. If you just use -p 8888, a random port on the host will be assigned.

The ./run_jupyter.sh tells docker what to execute within the container.

With this command, I can use a browser on the host machine to connect to http://localhost:8888/ and access the jupyter notebook.

UPDATE: After wrestling with docker on windows I switched back to a Ubuntu machine with docker. My notebook was being erased between docker sessions which makes sense after reading more docker documentation. Here is an updated command which also mounts a host directory within the container and starts jupyter pointing to that mounted directory. Now my notebook is saved on the host and will be available next time start up tensorflow.

docker run -p 8888:8888 -p 6006:6006 -v /home/rob/notebook:/notebook b.gcr.io/tensorflow/tensorflow sh -c "jupyter notebook /notebook"
0
5

Jupyter now has a ready to run Docker image for TensorFlow:

docker run -d -v $(pwd):/home/jovyan/work -p 8888:8888 jupyter/tensorflow-notebook

4

These steps worked for me if you are a total docker noob using a windows machine.

Versions: Windows 8.1, docker 1.10.3, tensorflow r0.7

  1. Run Docker Quickstart Terminal
  2. After it is loaded, note the ip address. If you can't find it use this docker-machine ip and make a note. Lets call it 'ip address'. Will look something like this: 192.168.99.104 (I made up this ip address)
  3. Paste this command on the docker terminal:

    docker run -p 8888:8888 -p 6006:6006 b.gcr.io/tensorflow/tensorflow.

    If you are running this for the first time, it will download and install the image on this light weight vm. Then it should say 'The Jupyter notebook is running at ....' -> This is a good sign!

  4. Open your browser at: <your ip address (see above)>:8888. Eg. 192.168.99.104:8888/
  5. Hopefully you can see your ipython files.
2

To get this to run under hyper-v. Perform the following steps:

1) Create a docker virtual machine using https://blogs.msdn.microsoft.com/scicoria/2014/10/09/getting-docker-running-on-hyper-v-8-1-2012-r2/ this will get you a working docker container. You can connect to it via the console or via ssh. I'd put at least 8gb of memory since I'm sure this will use a lot of memory.

2) run "ifconfig" to determine the IP address of the Docker VM

3) On the docker shell prompt type:

docker run -p 8888:8888 -p 6006:6006 -it b.gcr.io/tensorflow/tensorflow

4) Connect to the Jupyter Workbench using http:/[ifconfig address]:8888/

3
  • This was on Windows Server 2012-R2
    – kariato
    Commented Apr 7, 2016 at 14:42
  • Thing change fast. I found the following docs.docker.com/machine/drivers/hyper-v a much easier start up than step 1. Also if you are using windows 10, docker now has a native tool set with GUI tools here docker.com/products/docker#/windows
    – kariato
    Commented Jul 13, 2016 at 20:43
  • --do you mean "ipconfig" ifconfig doesn't work. When your in the boot2docker VM it's linux so windows commands don't work. ifconfig is the linux version of the ipconfig windows command. The VM will have it's own IP address since it runs it's own IP stack. Nice think about docker is it shares the VM IP stack so that address does not change.
    – kariato
    Commented Jul 13, 2016 at 20:45
1

To tidy up the things a little bit, I want to give some additional explanations because I also suffered a lot setting up docker with tensorflow. For this I refer to this video which is unfortunately not selfexplanatory in all cases. I assume you already installed docker. The really interesting general part of the video starts at minute 0:44 where he finally started docker. Until there he only downloads the tensorflow repo into the folder, that he then mounts into the container. You can of course put anything else into the container and access it later in the docker VM.


  1. First he runs the long docker command docker run –dit -v /c/Users/Jay/:/media/disk –p 8000 –p 8888 –p 6006 b.gcr.io/tensorflow/tensorflow. The “run” command starts containers. In this case it starts the container “b.gcr.io/tensorflow/tensorflow”, whose address is provided within the tensorflow docker installation tutorial. The container will be downloaded by docker if not already locally available. Then he gives two additional kinds of arguments: He mounts a folder of the hostsystem at the given path to the container. DO NOT forget to give the partition in the beginning (eg. "/c/"). Additionally he declares ports being available later from the host machine with the params -p. From all this command you get back the [CONTAINER_ID] of this container execution! You can always see the currently running containers by running “docker ps” in the docker console. Your container created above should appear in this list with the same id.


  2. Next Step: With your container running, you now want to execute something in it. In our case jupyter notebook or tensorflow or whatever: To do this you make docker execute the bash on the newly created container: docker exec –ti [CONTAINER_ID] bash. This command now starts a bash shell on your container. You see this because the “$” now changed to root@[CONTAINER_ID]:. From here is no way back. If you want to get back to the docker terminal, you have to start another fresh docker console like he is doing in minute 1:10. Now with a bash shell running in the container you can do whatever you want and execute Jupiter or tensorflow or whatever. The folder of the host system, you gave in the run command, should be available now under “/media/disk”.


  3. Last step accessing the VM output. It still did not want to work out for me and I could not access my notebook. You still have to find the correct IP and Port to access the launched notebook, tensorboard session or whatever. First find out the main IP by using docker-machine –ls. In this list you get the URL. (If it is your only container it is called default.) You can leave away the port given here. Then from docker ps you get the list of forwarded ports. When there is written 0.0.0.32776->6006/tcp in the list, you can access it from the hostmachine by using the port given in the first place (Awkyard). So in my case the executed tensorboard in the container said “launched on port 6006”. Then from my hostmachine I needed to enter http://192.168.99.100:32776/ to access it.

-> And that’s it! It ran for me like this!

0

It gives you the terminal prompt:

FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd vdocker') DO %i
docker run -it tensorflow/tensorflow:r0.9-devel

or

FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd vdocker') DO %i
docker run -it b.gcr.io/tensorflow/tensorflow:latest-devel

You should have 'vdocker' or change vdocker to 'default'.

0

For some reason I ran into one additional problem that I needed to overcome beyond the examples provided, using the --ip flag:

nvidia-docker run --rm \
  -p 8888:8888 -p 6006:6006 \
  -v `pwd`:/root \
  -it tensorflow/tensorflow:latest-devel-gpu-py3 sh -c "jupyter notebook --ip 0.0.0.0 ."

And then I can access via http://localhost:8888 from my machine. In some ways this makes sense; within the container you bind to 0.0.0.0 which represents all available addresses. But whether I need to do this seems to vary (e.g I've started notebooks using jupyter/scipy-notebook without having to do this).

In any case, the above command works for me, might be of use to others.

0

As an alternative to the official TensorFlow image, you can also use the ML Workspace Docker image. The ML Workspace is an open-source web IDE that combines Jupyter, VS Code, TensorFlow, and many other tools & libraries into one convenient Docker image. Deploying a single workspace instance is as simple as:

docker run -p 8080:8080 mltooling/ml-workspace:latest

All tools are accessible from the same port and integrated into the Jupyter UI. You can find the documentation here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.