I understood docker's -t as a kind of virtual terminal that seems to access the terminal through /dev/pts. So, if I do echo "hello, tty" > /dev/pts/1
, I see that it is output to the connected terminal. Since the -i option is STDIN, the container understands it as an option to receive text as input. So, who does the input go to when only the -i option is applied?
Below is the result of the command given only the -i option.
~ $ docker exec -i mysql-container bash
tty
not a tty
ls
bin
boot
dev
docker-entrypoint-initdb.d
entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
I didn't give the -t option, so I was expecting no results back. But it exactly missed my expectations. Why is it running like this?
CodePudding user response:
The difference is subtle, but when you only use -i
, you communicate with it using stdin and stdout. But there's no terminal in the container.
When you use -it
you attach a terminal and that lets you do 'terminal stuff'. For instance, with -it
- you get a prompt
- you can send programs to the background with stuff like
tail -f /dev/null &
- ctrl-c works as expected
- etc
The difference is hard to spot, because with -i
you usually take stdin from a terminal on the host and send stdout to the a terminal on the host.
Usually, when you want to run commands interactively, you'll use -it
. A scenario where you might use only -i
is when you pipe the commands into the container. Something like this
echo -e 'tty\nls' | docker exec -i mysql-container bash
which will run the tty and ls commands and give you the output.