-1

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

1
  • You'd be better off implementing a simple webserver, and then exposing this port, IMO. Edit, I see you say you don't want to modify the image, but there's nothing stopping you from building another image with this as the base. Commented Jun 19, 2024 at 11:39

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

I've just tried your recommendation but I can't figure out how to send the command to stdin. the stdout and stderr is working. Basically I want to recreate docker attach CONTAINERID with this command I can type status in the terminal and then get the status back from the running server.
@ShaneVolpe seems the python sdk's attach method dose not support stdin, in go sdk, it looks like this: ` r, _ := cli.ContainerAttach(ctx, containerName, types.ContainerAttachOptions{ Stdin: true, Stdout: true, Stderr: true, }) r.Conn.Write([]byte("status"))`
@ShaneVolpe stackoverflow.com/questions/26843625/… maybe this is what you want
The only thing I don't like about this approach is it uses a protected member (_sock). If there is a better way to do this please let me know!
0

Here is my final solution for sending to stdin and receiving from stdout:

setting up and running the docker image

import docker
client = docker.APIClient()
container = client.create_container("nwnxee/unified", name='test', stdin_open=True)
client.start(container)

Sending to stdin with a socket:

s=client.attach_socket(container, params={'stdin': 1, 'stream': 1, 'stdout':1})
s._sock.send('status'.encode())

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.

s._sock.setblocking(False)
data=s._sock.recv(1000)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.