0

I have a simple python code I want to run inside a docker container.

print("Starting")
while True:
    stdin = input()
    print('read: ', stdin)
    if stdin == 'end':
        break
print("Closing")

The docker container is an instance of an image containing the script. Dockerfile:

FROM continuumio/anaconda3 
ADD script.py /
CMD [ "python", "/script.py" ]

When I run

docker run -i myimage

Everything works as expected. I have access to the stdin and I can communicate with the script.

I would like to replicate this behavior in python via either dockerpy or Popen.

I thought something like this will work, but it doesn't

p = Popen(['docker', 'run', '-i', '-d', 'myimage'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
p.stdin.write("Hello world!")
p.stdin.write("end")
Hudler
  • 23
  • 3
  • If you're set on using Docker here, you will find it easier to interact with your process using a network API than stdin; you might try wrapping it in something like a Flask wrapper. Then you can use a library like Requests to make calls into it, without needing to know that Docker is involved (and needing the privileges that requires). – David Maze Aug 06 '20 at 16:01
  • I want to use Docker for sandboxing the script. Network API seemed like overkill at first but I might have to resort to that solution. – Hudler Aug 06 '20 at 17:57

0 Answers0