问题描述
我试图运行一个简单的Docker容器,Tensorflow可用(首先是CPU)。我认为只有一次设置Dockerimage是一个好主意(即每次运行容器时都不会更新张量流版本)。为此,我建议做我的Docker文件(来自源的评论来自我的建议):
#这意味着你得到你的来自张量流码头码头的码头图像
FROM gcr.io/tensorflow/tensorflow
然而,当我运行我的Docker容器时,我做了 pip list
,并没有看到Tensorflow可用的任何地方,当我运行我的脚本我有一个熟悉的错误:
ImportError:没有名为'tensorflow'的模块
我想到了一种解决方法,只需让我的Dockerfile明确地 pip3 install
tensorflow。我打算制作一个bash脚本,让Dockerfile运行它:
#bash script intall_tensorflow.sh
#在容器中安装Tensorflow
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
然后只需添加到docker文件:
运行sh intall_tensorflow.sh
然而,我的直觉告诉我,这可能是错误的或太黑客。如果我稍后要手动安装Tensorflow,为什么要首先需要张量流基础图像 FROM gcr.io/tensorflow/tensorflow
?
我尝试在线研究什么 gcr.io/tensorflow/tensorflow
可能在做,但我没有找到任何超级有用。有人知道在图像本身(即从构建Docker图像)的Docker容器中可以使用Tensorflow的正确方法是什么?
对不起,如果我真的很密,但只是觉得我做错了,我找不到在线的东西来解决我的问题。
在看完答案后,似乎主要的问题可能是python 3因为某种原因找不到tensorflow,而是python 2可以。这是否意味着我需要直接安装TensorFlow(在Docker镜像中使用pip)才能使用正确版本的TensorFlow?
user @ computer:〜$ docker run - 它是gcr.io/tensorflow/tensorflow / bin / bash
root @ 61bb0f99582b:/ notebooks#python
Python 2.7.6(默认,2016年10月26日,20:30:19)
[ GCC 4.8.4] on linux2
有关详细信息,请输入help,copyright,credits或license。
>>> import tensorflow
>>>
root @ 61bb0f99582b:/ notebooks#python3
Python 3.4.3(默认,2015年10月14日,20:28:29)
[GCC 4.8.4] on linux
类型帮助,版权,信用或许可证了解更多信息。
>>> import tensorflow
追溯(最近的最后一次调用):
文件< stdin>,第1行,< module>
ImportError:没有名为'tensorflow'的模块
>>>
如果由于某些原因导致您的问题,您还可以自己按照您所描述的方式安装。关于docker的一件好事是,当它们从Dockerfiles创建它时,它会缓存图像,所以每次构建一个图像时,最终都不会重新安装张量流。 解释了一些的概念。
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?
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.
这篇关于在码头箱容器或码头工作人员图像中使用Tensorflow的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!