As it turns out, you can assign a pseudo tty when using
docker run and still put it into the background and I’ll show you how and why you might want to do so!
You probably know the following
docker run parameters:
-t : Allocate a pseudo-tty
-i : Keep STDIN open even if not attached
And you probably also know
-d to start
docker run in a detached state (background).
But did you know you can use them all together?
This means you can start a process in docker, assign a pseudo tty to it, put it into the background but keep STDIN open.
Imagine you have a daemon application that outside of docker normally runs in the foreground and expects a tty (terminal) with input, so that you can give it commands while the process is running. Normally, when using
docker run you will make it run in the background with
-d and often times you don’t have to add a terminal or input to it.
In most cases, you’ll be fine putting it into the background and forget about it. But what if you want to quickly get a terminal with input to give it commands? Well, that’s why you should have used docker run with -t and -i because then you can do this:
docker run -tid --name my-docker-thing container-name daemon-command
Now that you want a terminal:
docker attach my-docker-thing
Then you pass your commands as you normally would and do your stuff and when you’re done, you detach it again with:
Which basically means, press
CTRL + P followed by
CTRL + Q and it will be detached again.
An example of where you might want to use docker in this way is when running something like a Minecraft server in docker so that you can run it in the background but still attach it, to pass commands to it, like adding users to a whitelist and then detach it again.
Want to know how to run Minecraft server in docker? Follow my guide here!