Standard input EOF error when creating a Python container using docker-py

31 views Asked by At

I am a beginner in this field.

I used docker-py to create a container as shown below.
The reason why the command is bash is because the code to install the necessary packages with for and to customize the startup command is below.
I want to pipe stdio in the container with tcp, and we will implement that later.

base = "python:3.7.12"
limit = {"ram": "1g"}

container = docker.from_env().containers.run(
    base,
    command="bash",
    detach=True,
    ports=port[0],
    name=assignee,
    #tty=True,
    stdin_open=True,
    stdout=True,
    stderr=True,
    **dict(limit))

# Dependencies install and more...
container.exec_run("echo 'test' > test.txt")

container.exec_run("python foobar.py")

foobar.py

text = input(">>")
print(text)

Run this code to run the container, and you will get this exception in the container.

Traceback (most recent call last):
  File "foobar.py", line 1, in <module>
    text = input(">>")
EOFError: EOF when reading a line

How can I resolve this error? My ideas:

  1. Use something like dockerpty to create a pseudo-tty.
  2. Not using Docker

I am using a translator, so if you have any questions, please comment and I will do my best to answer them. If you have any questions or comments, please comment to me.

Thank you.

0

There are 0 answers