1

I was trying to run a simple docker container with Tensorflow available (first with CPU). I thought it would be a good idea to setup my Dockerimage only once (i.e. not update the tensorflow version every time I run a container).

To do this I was suggested to do as follow in my Dockerfile (the comment came from source that gave me the suggestion):

# This means you derive your docker image from the tensorflow docker image
FROM gcr.io/tensorflow/tensorflow

however, when I ran my Docker container I did pip list and didn't see Tensorflow available anywhere plus when I ran my script I got the familiar error:

ImportError: No module named 'tensorflow'

I thought of a way to solve this by just having my Dockerfile explicitly pip3 install tensorflow. I planned to make a bash script and have my Dockerfile run it:

# bash script intall_tensorflow.sh 
# to install Tensorflow in container
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc1-py3-none-any.whl
pip3 install --upgrade $TF_BINARY_URL

and then just add to the docker file:

RUN sh intall_tensorflow.sh

however, my intuition tells me this might be wrong or too hacky. Why would I need the tensorflow base image FROM gcr.io/tensorflow/tensorflow in the first place if I am just going to manually install Tensorflow later anyway?

I tried researching online what gcr.io/tensorflow/tensorflow might be doing but I have not found anything super useful. Does someone know what is the proper way to have Tensorflow available in a Docker container from the image itself (i.e. from building the Docker image)?

Sorry if I'm being really dense but it just feels I'm doing something wrong and I couldn't find something online that addressed my question.


After looking at the answer it seems that the main issue might be that python 3 cannot find tensorflow for some reason but python 2 can. Does that mean that I need to directly install TensorFlow myself (with pip in the docker image) for the right version of TensorFlow to be available?

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323

1 Answers1

1

Judging by your usage of pip3 - are you using python 3? That might be causing your issues. I tried to recreate your problem, but python 2 seems to be working fine.:

user@computer:~$ docker run -it gcr.io/tensorflow/tensorflow /bin/bash
root@61bb0f99582b:/notebooks# python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>>
root@61bb0f99582b:/notebooks# python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'tensorflow'
>>>

If this for some reason still causes you issues, you can also just install it yourself the way you describe. A good thing about docker is that it caches images when it creates them from Dockerfiles, so you don't end up reinstalling tensorflow every time you build an image. This article explains some of the concepts.

Robert Lacok
  • 4,176
  • 2
  • 26
  • 38
  • I replicated your suggestion. It seems that when I do python3 it does not recognize that tensorflow is installed. Maybe `gcr.io/tensorflow/tensorflow` uses tensorflow binary for python 2. Do you know if thats true? Also, is there no other way except manually (as I suggested), to install tensorflow for python 3 in docker? – Charlie Parker Dec 16 '16 at 01:03
  • also, why do you need the bash command at the end of your docker run, it seems that without it this specific image freaks out with some weird notebook warnings that seem to have security issues. However, it seems that running containers without bash from other images is fine. Do you know why sometimes bash is required and sometimes its not? – Charlie Parker Dec 16 '16 at 01:06
  • 1
    Yes there is actually, just use an appropriate tag, so for example gcr.io/tensorflow/tensorflow:latest-py3 . For a list of different tags you can look for example here: https://hub.docker.com/r/tensorflow/tensorflow/tags/ or just follow the the gcr.io/tensorflow/tensorflow link in your browser – Robert Lacok Dec 16 '16 at 09:35
  • 1
    The reason for using /bin/bash is for getting into the bash of the container. If you inspect the [Dockerfile](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/docker/Dockerfile) for the tensorflow/tensorflow image you will find that it ends with CMD ["/run_jupyter.sh"], which presumably runs a jupyter notebook. If this is not specified, it defaults to bash. More details [here](https://docs.docker.com/engine/reference/builder/#/cmd) – Robert Lacok Dec 16 '16 at 09:47