Charlie Parker
Charlie Parker

Reputation: 5169

What is the correct way to have Tensorflow available in a docker container or docker image?

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?

Upvotes: 1

Views: 1619

Answers (1)

Robert Lacok
Robert Lacok

Reputation: 4324

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.

Upvotes: 1

Related Questions