“pythonsetup.py安装”不能从Dockerfile工作,但我可以在容器中,并做同样的..任何指针?

我正在执行“sudo docker build”时出现此错误。

> (3:58:02 PM) njain: tep 28 : RUN python /tmp/setup.py install && > python /tmp/buzz/scripts/setuprabbit.py ---> Running in e7afcbda3c75 > Traceback (most recent call last): File "/tmp/setup.py", line 7, in > <module> > long_description=open('README.md', 'r').read(), IOError: [Errno 2] No such file or directory: 'README.md' 2014/10/15 15:40:14 The command > [/bin/sh -c python /tmp/setup.py install && python > /tmp/buzz/scripts/setuprabbit.py] returned a non-zero code 

我的Dockerfile看起来像这样:

 ADD buzz /tmp/ # DOCKER-VERSION 0.3.4 #bunch of installs RUN cd /tmp/ RUN python /tmp/setup.py install && python /tmp/buzz/scripts/setuprabbit.py 

当我在容器(交互式的shell和CD到/ tmp /我能够做没有任何问题的python setup.py安装)

docker文件中的每一步:

  • 创build一个容器
  • 以某种方式改变它
  • 提交结果(通常)创build一个新的图像
  • 删除容器。
  • 在下一步中使用新的图像

所以你的docker文件说:

 ADD buzz /tmp/ # change the container to have this new file # DOCKER-VERSION 0.3.4 #bunch of installs RUN cd /tmp/ # don't change the container at all and then save the results RUN python /tmp/setup.py install && python /tmp/buzz/scripts/setuprabbit.py ## do the install 

所以第二行到最后一行的cd命令什么也不做,并且不会影响后面的行。 这是一个不幸的副作用,使docker文件看起来太像shell脚本,他们不是。 而是cd命令放在需要使用它的同一行上,这样它的效果将沿着这条线向前传递,而不是在同一个Dockerfile中的其他RUN命令

 RUN cd /tmp/ && python /tmp/setup.py install && python /tmp/buzz/scripts/setuprabbit.py 

Arthur的答案正确地确定了问题的原因,并提供了一个有效的解决scheme。

然而,Docker的“写入Dockerfiles的最佳实践”build议不要使用他所build议的模式(即RUN cd /some/path && do-some-command ),而是build议使用WORKDIR指令(这是为了解决这个确切的用例)。

WORKDIR基本工作原理是如何使您的cd命令工作:它改变工作目录,保留新的工作目录以备后面的指令在Dockerfile中使用。

在你的情况下,生成的Dockerfile将如下所示:

 ADD buzz /tmp/ # DOCKER-VERSION 0.3.4 #bunch of installs WORKDIR /tmp/ RUN python /tmp/setup.py install && python /tmp/buzz/scripts/setuprabbit.py