1

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.

Peyang
  • 77
  • 8
  • You should probably use a Dockerfile to install packages to build a reusable image. I'd avoid scripting `docker exec` if possible. – David Maze Sep 29 '21 at 10:31
  • Thanks for the comment, I don't think creating a Docker image is suitable for this purpose. The above method worked. Thank you very much. – Peyang Sep 29 '21 at 19:18

0 Answers0