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")