gpt4 book ai didi

python - 使用 python 代码创建 Docker 容器时出现问题

转载 作者:行者123 更新时间:2023-12-01 00:53:35 24 4
gpt4 key购买 nike

我对 python 和 docker 都很陌生,但尽管如此,我正在尝试为我编写的密码生成器应用程序创建一个 Docker 容器。但在构建应用程序后,我收到错误消息,我不知道它们是否与 python 代码或我构建 Docker 的方式有关。

我希望应用程序能够正常运行,但我收到了以下错误消息:

URL? >>  Traceback (most recent call last):
File "pwd1.py", line 6, in <module>
url = input('URL? >> ')
EOFError: EOF when reading a line

这在我的电脑或 Sublime 中从来都不是问题,应用程序始终正常运行。

这是原始代码(变量“a”末尾的“(...)”是由于 Nano 没有转录可见屏幕之外的其余 carachters):

import random

a = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G','g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p'(...)]

file = open('/home/mic/python/password-generator/list.py', 'a')
url = input('URL? >> ')
file.write(url)
file.write(' - ')
k: int = int(input('How long? >> '))
b = (str(''.join(random.sample(a, k))))
print(b)
file.write(b)
file.write('\n')
file.close()

研究错误后,我将代码更改为:

import random

a = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G','g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p'(...)]

file = open('list.py', 'a')
url = input('URL? >> ')
while True:
try:
line = input()
except EOFError:
print ("EOFError")
file.write(url)
file.write(' - ')
k: int = int(input('How long? >> '))
b = (str(''.join(random.sample(a, k))))
print(b)
file.write(b)
file.write('\n')
file.close()

但我收到了同样的错误消息。我在 sublime、计算机和在线编辑器上尝试了这段代码,没有人提示它。

正是因为这个问题,我开始思考它是否与我的 Docker 文件有关。

这是我的 Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.7.3

# Set the working directory to /app
WORKDIR /password

# Copy the current directory contents into the container at /app
COPY . /password

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "pwd1.py"]

我按照此处解释的说明进行操作 https://docs.docker.com/get-started ,当我尝试使用“sudo docker run pwdmanager”运行应用程序时,我收到上述错误消息。

任何帮助将不胜感激

最佳答案

请使用 -i 标志(交互式)运行您的 Docker 容器。

示例:

docker run -i -t <your-options>

当然,这将留下最大的问题,正如@MisterMiyagi 的评论中正确指出的那样:

input asks for input on stdin, for example from a terminal. How do you provide this in the container? Your docker file seems not to include any input for pwd1.py

为了解决这个问题,我建议您阅读this answer on SO .

或者,恕我直言,在这个用例中跳过使用 Docker。

关于python - 使用 python 代码创建 Docker 容器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com