简体   繁体   English

docker python脚本无法正确执行

[英]docker python script doesn't execute properly

When I run my container I'm trying to run "docker.py" from the root directory ("/") with "/usr/bin/python". 当我运行容器时,我尝试使用“ / usr / bin / python”从根目录(“ /”)运行“ docker.py”。 I've checked the source image to ensure the python binary exists, I've uploaded docker.py to the docker image and run it with that binary successfully. 我已经检查了源映像以确保python二进制文件存在,我已将docker.py上传到docker映像并成功使用该二进制文件运行它。 However, when I try to create the image and then run the image, I get this error ("EOFError: EOF when reading a line") and I don't know what this means or where to start. 但是,当我尝试创建映像然后运行该映像时,出现此错误(“ EOFError:读取行时出现EOF”),我不知道这是什么意思或从哪里开始。 Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance! 提前致谢!

cat docker.py 猫docker.py

#!/usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()

cat Dockerfile 猫Dockerfile

FROM python:2.7
ADD docker.py /docker.py
CMD ["/usr/bin/python", "/docker.py"]

docker build . 码头工人建设。

Sending build context to Docker daemon  175.8MB
Step 1/3 : FROM python:2.7
 ---> b1d5c2d7dda8
Step 2/3 : ADD docker.py /docker.py
 ---> f55a19158773
Step 3/3 : CMD ["/usr/bin/python", "/docker.py"]
 ---> Running in b830da5a2f5b
 ---> ef5b878d203f
Removing intermediate container b830da5a2f5b
Successfully built ef5b878d203f
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

docker run ef5b878d203f docker运行ef5b878d203f

Enter your name: Traceback (most recent call last):
  File "/docker.py", line 2, in <module>
    my_name = raw_input("Enter your name: ")
EOFError: EOF when reading a line

The trouble is that your docker.py script is expecting input. 问题在于您的docker.py脚本需要输入。 As @Klaus D. was commenting, for the raw_input command you need to be in an interactive shell. 正如@Klaus D.所评论的那样,对于raw_input命令,您需要位于交互式外壳中。 When you're running inside a docker container, there is no interactive shell, so when you run raw_input it just gets an unexpected end to that command hence the error you receive. 当您在docker容器中运行时,没有交互式外壳,因此当您运行raw_input时,它只会意外终止该命令,因此会收到错误。

Once you start the container, from a user perspective it is like a little virtual machine, the only interaction you can have with it is through methods that you have set up within the container itself. 一旦启动了容器,从用户的角度来看,它就像一台小型虚拟机,与容器的唯一交互是通过在容器本身内设置的方法。 Or else to enter the container using: 否则使用以下命令进入容器:

docker exec -it CONTAINER_HASH /bin/bash

from there you've got an interactive shell and if you log in like that THEN you can run 从那里您有一个交互式外壳,如果您以这种方式登录,则可以运行

python /docker.py

and it works as you would expect. 它可以像您期望的那样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM