Docker py Stdin 在容器内不工作

Docker py Stdin not working inside container

提问人:Gabriel Knies 提问时间:11/2/2023 更新时间:11/2/2023 访问量:22

问:

我正在尝试在 python 中制作一个 API,该 API 使用 dockerpy 执行用户给出的一些代码。

到目前为止,我能够执行不需要与 stdin 交互的简单代码,例如在 python 中。print("hello world")

但是,一旦我尝试在 docker 容器中使用 stdin,我最终一无所获。

这是我的代码: (program.py 和 input.txt 文件存在于容器内,不为空。这部分有效)

输入 .txt 如下所示:

hello

program.py 看起来像:

a = input()
print('a: ', a)

这是我的代码:

log = client.containers.run(
  image=IMAGE_NAME,
  command="timeout -s 10 5 python3 program.py < input.txt",
  volumes={source_file_host: {'bind': source_file_guest, 'mode': 'rw'}},
  working_dir=source_file_guest,
  stdin_open=True
)
print(log)

当我尝试以 program.py 中的代码运行程序时,我确实得到了“hello world”作为输出。但是,当我用作代码时,我没有收到任何东西print("hello world")

a = input()
print('a: ', a)

当我删除超时时,我没有得到任何东西,因为容器仍在运行,因为 program.py 仍在等待输入

超时后,我收到错误:Command 'timeout -s 10 5 python3 program.py < input.txt returned non-zero exit status 124'

我也尝试了作为命令,但我得到了相同的结果。timeout -s 10 5 python3 program.py <<< "salut"

如何在容器内提供 stdin 输入?

python docker stdin dockerpy

评论


答: 暂无答案