简体   繁体   English

尝试将Python与Docker集成。 在代码的输入部分出现错误

[英]Trying to integrate Python with Docker. Getting error on input portion of code

I have a very simple python script. 我有一个非常简单的python脚本。 I am trying to use it in a docker container. 我正在尝试在Docker容器中使用它。

Python file (computer.py) Python档案(computer.py)

import datetime
print("Welcome to virtual assistant.")
name = input("What is your name? ")
print("Welcome " + name)
def time():
print(datetime.datetime.now())
command = input("Would you like to know the time " + name + "?")
if command == "yes":
print(time())

And My docker file looks like this.(Docker) 我的docker文件看起来像这样(Docker)

FROM python:3

ADD computer.py /

CMD [ "python", "./computer.py" ]

Then I ran 然后我跑了

docker build -t python-barcode .

Then 然后

run python-barcode

I get this error 我得到这个错误

Traceback (most recent call last):
  File "./computer.py", line 4, in <module>
    name = input("What is your name? ")
EOFError: EOF when reading a line
Welcome to virtual assistant.
What is your name? %

It seems to run the code up untill I ask for input? 似乎要运行代码直到我要求输入? Not even sure what would cause that. 甚至不确定是什么原因造成的。 Any help would be greatly appreciated. 任何帮助将不胜感激。

When you run a container with docker run , stdin is by default not connected, so anything that attempts to read interactive input will fail. 当您使用docker run运行容器时,默认情况下不会连接stdin ,因此任何尝试读取交互式输入的操作都会失败。 You probably want to run: 您可能要运行:

docker run -it python-barcode

The -i leaves stdin connected, and the -t allocates a tty which is what you normally want to interactive input. -i使stdin保持连接状态, -t分配一个tty,这是您通常希望进行交互式输入的内容。

您可以使用-i和-t以交互方式运行容器

docker run -it python-barcode

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

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