1

To summarize the problem I have: I want to execute a command on the minecraft console that is running in the container like when I attach to it in interactive mode but without the need to attach to it.

docker attach container_name

command

detach_from_contaienr

Like running docker exec but it puts the command into the stdin of the programm that is running inside the container like in docker attach.

I simply search a oneliner that does the same. Like in this question

Edit: echo 'say test' | docker attach <container id>

Gives the Error:

the input device is not a TTY

Edit2: after removing the -t flag on the container linke in this post

echo 'say test' | docker attach <container id>

the command gets to the server as the log reveales but after executing that I am stuck in a blank input because the command doesn't stop somehow

If i now do the double ctrl+c the container stops...

Edit3: I try to execute these commands on the docker host and execute the command in the running spigot minecraft server

  • You can't do this in standard Linux; I wouldn't attempt it in Docker either. Would a network (HTTP) interface be a better match for your application, so that you can send it requests and get responses back without trying to manage the container's stdin (and without the requirement to be root to interact with it)? – David Maze Apr 28 '20 at 14:25
  • Thanks for your answer! The Problem is that it is not a self developed application but a minecraft server. And I simply want to write to its console. I edited the question so it is clear that it is a minecraft server and not a random application. – Checker 8763 Apr 28 '20 at 14:33

1 Answers1

-1

Apparently, you can use a named pipe to do this, as shown here: https://stackoverflow.com/a/26765590/2926055

# in the Docker container
$ mkfifo myfifo
$ java -jar minecraft_server.jar nogui < myfifo

# via your `docker exec`
$ echo 'say test' > myfifo

As noted, be careful you don't accidentally send an EOF character.

user2926055
  • 1,963
  • 11
  • 10
  • This answer is perfecly fine but not quite the answer I would like to use because I need to rebuild the image and need extra logic to do this. – Checker 8763 Apr 28 '20 at 18:57
  • Unfortunately for you, the only way to write to the server's stdin is to use a named pipe, which means you have to change the way the .jar is launched. It's not possible to "steal" stdin for a process, because that would immediately destroy the entire modern computing world. – user2926055 Apr 28 '20 at 19:09
  • Yes I understand that :D The only thing that bothers me that when I set the -i and -t flag and attach manually and execute a command it's no problem. But when I use the echo commd | container method it says that is is no tty device. So Docker uses the stdin but does not provide a nice way to write into that. – Checker 8763 Apr 28 '20 at 19:15
  • Ah wait, Is the Docker container already launching the .jar with `nogui`, and so you're just trying to hook up `docker`'s stdin to the .jar stdin? – user2926055 Apr 28 '20 at 19:35
  • Yes ! I want write it to the stdin of my runnning spigot container. – Checker 8763 Apr 28 '20 at 19:42
  • I'm not familiar enough with Java's handling of stdin or the specific Docker image you're using to help further, but your `echo | docker attach` example seems like it should work. Double check if you need to send some other line-ending character with a `printf | docker attach` instead....? – user2926055 Apr 28 '20 at 19:48
  • Downvote for "be careful you don't accidentally send an EOF character". There's no such thing as sending an EOF character. – Joseph Sible-Reinstate Monica Apr 29 '20 at 00:50