I have a game server that I’m running in a docker image. If you send ‘status’ to the server stdin it will send the status of the server to it’s stdout. I’m trying to find a way to send ‘status’ to the server from outside of docker. I want to send the ‘status’ command anytime I want to get an update from the game server. Ideally I would love to use either docker-python sdk or Python on whales but if I have to do it manually via the shell I’m ok with that too. I’m hoping not to have to modify the docker image because it’s provided by the game manufacturer
I’ve looked into attach_socket but I believe that only sends stdout and stderr and does not provide an interface for sending to stdin.
I’ve also looked into this approach:
Sending command to minecraft server running in docker
but since I’m wanting to do this using python the sending of ctrl-c when I’m finished is a bit hacky. I’m hoping there is a cleaner way.
I’m leaning toward this approach:
https://serverfault.com/questions/885765/how-to-send-text-to-stdin-of-docker-container
but it still feels a little hacky to me and it’s a Linux only solution where I would like my project work on Windows and Linux
2
Answers
Here is my final solution for sending to stdin and receiving from stdout:
setting up and running the docker image
Sending to stdin with a socket:
reading stdout from the socket. I turned off blocking so I did not have to set up a thread just for receiving. In a real design (if your not using blocking) you probably would want to use a loop around the recv with a try/except, the except will occur when the buffer is empty.
take a look here: https://docs.docker.com/reference/cli/docker/container/attach/
you should use
docker run -it
to start the container which will keep stdin open, then you can attach to it and write to stdin.in python sdk it is
Container.attach(**kwargs)
https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.Container.attach
update: the attach method dose not support stdin,you should use attach_socket instead, please refer to How to send to stdin of a docker-py container?