Trying to understand the relation of tty between the host and container when the docker run command is executed

My old reply in another topic could be useful here:

In your case, the TTY is not for having an output. Some programs like a shell need a pseudo TTY to be able to run. You run the following command:

docker run -d --name bash-1 bash

It will run and stop immediately. Now run this:

docker run -d --name bash-2 -t bash

It will keep running. Or just try to run it in interactive mode:

docker run --rm -i bash

You can type characters, but there is nothing to handle what you typed without the TTY.

Or you can run this:

echo 'hello' | docker run --rm -i bash cat

You still don’t have TTY, but you don’t need because the content is sent to the standard input and cat reads that, not what you type.

That is why we often use -it together because most of the time we just want to get a shel in the container which requires both. Having an interactive mode and also a TTY. Using only -t kept the container alive, but it was useful only because it ran in the background in detached mode. If you try it in the foreground (attached mode), you will get a bash shell, but when you type “exit” it will not exit and press enter, it will not exit. And when you try to run “read test” in the shell, it will do nothing and you won’t get the prompt back by pressing enter, only by pressing CTRL+C.

docker run --name mustbekilled -t bash

So you will need another terminal and run:

docker rm -f mustbekilled

And when you run only:

docker run --name noprompt -i bash

You will get no prompt in the shell, but you can type commands, but the read command will not work either. Fortunately the exit command will work in this mode.

2 Likes