Issue
we know that -it
is actually two flags -i
and -t
which means redirection from STDIN
and STDOUT
to the terminal repsectively.
Let's say I want to use redis-cli on a running redis container (says id is abc123) and do:
docker exec -i abc123 redis-cli // only attach to `STDIN` of redis-cli process
when I run a valid command like set key 1
, I got "OK" message return to my terminal. Looks like redis-cli container process can still return response("OK") to my terminal via STDOUT
, but I didn't attach my terminal to STDOUT
, did I?
So redis-cli container still return response to my terminal via STDOUT
?
Solution
The t
does not mean attach STDOUT, the t
means a pseudo TTY should be allocated, so that the container thinks it's writing to a TTY. Some programs have subtle differences whether they run in a TTY or not. Docker will use your terminal as TTY.
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
The interactive mode alone is useful if you want to have your container read from stdin but not necessarily use your shell as pseudo TTY. For example, when writing scripts.
$ echo foo | docker run -i busybox cat -
foo
Note there is also the attach
flag.
-a, --attach list Attach to STDIN, STDOUT or STDERR
This might behave more like you expect from -ti
.
Note that these two are related to each other, and docker will use defaults in some cases if certain other flags are present. This area of docker is a bit arcane and I recall some inconsistencies.
For example, one would expect when attaching STDIN directly, it would behave the same as when using the -i
flag.
In the below example, the second docker run doesn't print the content of the file
$ echo 'hello, world' > hello.txt
$ docker run -i busybox cat < hello.txt
hello, world
$ docker run -a STDIN -a STDOUT -a STDERR busybox cat < hello.txt
Answered By - The Fool Answer Checked By - Timothy Miller (WPSolving Admin)