Closing stdin on a Running Container
See original GitHub issueI am using docker version 2.1.0.
When starting a container the parameter stdin_open
can be used to prepare the stdin socket for writing. I have had success doing this with code along these lines,
stdin_data = 'some data to send to stdin'
container = client.containers.run(image, detach=True, stdin_open=True)
socket = container.attach_socket(params={'stdin': 1, 'stream': 1})
os.write(socket.fileno(), stdin_data.encode())
socket.close()
exit_code = container.wait()
I have also found that this approach can easily hang indefinitely, if the underlying container is waiting for input. The reason seems to be be that the socket status is independent of the stdin_open
parameter, so the code running in the container never receives an EOF-like sentinel on stdin.
Any suggestions on how to address this?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:7
Top Results From Across the Web
Docker closes STDIN despite "--interactive" - Server Fault
The issue I'm seeing is that closing the docker run command also closes STDIN. The container continues running and when using docker attach ......
Read more >Correct way to detach from a container without stopping it
Type Ctrl + p then Ctrl + q . It will help you to turn interactive mode to daemon mode.
Read more >docker attach - Docker Documentation
To stop a container, use CTRL-c . This key sequence sends SIGKILL to the container. If --sig-proxy is true (the default), CTRL-c sends...
Read more >Attach and Detach From a Docker Container - Baeldung
Pressing CTRL-c is the usual way of ending a session. But, if we've launched our container without the -d or -it option, the...
Read more >How do I close stdin in a shell script? - Super User
Is there a way to close stdin? Closing File Descriptors. n<&- Close input file descriptor n. 0<&- or <&- Close stdin. Source Chapter...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okay, I think I figured it out! The missing ingredient was a
_sock.close
call. So the complete example:Running against the Docker image above, which simply runs the following script, produces the following output:
So we can see that the
output.py
script receivedstdin
and successfully printed it!I believe the original issue can be resolved with the following:
The API ergonomics leave something to be desired here. Having the muck with
sock._sock
is not ideal, but this workaround appears to work as expected 👍Interestingly, it looks like there’s differences between Python 2 and Python 3.
If I remove
detach=True
the following code works as expected in Python 2:This is expected due to the Python 2 and 3 differences. However, changing to the following code produces different results:
So the question is: can we achieve the old Python 2 behavior from
_socket.socket
with Python 3’sSocketIO
?