Understanding the mysterious -t arg for docker run

-t by far is the worst documented command for docker run for beginners.

Don’t get me wrong, I don’t say this to criticize the docker community but to bring to attention that more needs to be done in explaining this command. Most beginners, like myself included prefer memorizing it instead of learning what it does under the hood, which leads to some funny scenarios in productions.

Having said that I hope you will help me in getting a more in-depth understanding of what -t does and how it interacts with its companion -i.

-i Means keep the STDIN open even through it is not attached.
-t Allocates a pseudo terminal.

I have an idea as to what they do independently but I cannot understand what happens when they are clubbed together.

When I do:

docker run -it alpine cat

Here is my understanding of what happens:

  1. 1 ) Docker starts and attaches the STDIN to the input of cat

  2. 2 ) A tty is spun up and the STDOUT is connected to the STDIN of the input stream made for cat

  3. 3 ) Thus whatever I type will go to cat.

First of all am I right ?

I don’t think I am because if I do

docker run -i alpine cat

omitting the -t gives me near identical behaviour, so what is -t actually doing ?

Also where is the pseudo terminal coming from? Is it from the system or the from docker ?

I tried reading the source but it didn’t answer my questions.

Thanks in advance.