0

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?

Hwan E
  • 566
  • 2
  • 13
  • `if I do echo "hello, tty" > /dev/pts/1` Sure, and if you do `echo "hello, tty"` without redirection, the output is _also_ in your terminal. – KamilCuk Jan 15 '23 at 13:02
  • What's your actual task? (If you weren't using Docker, would you try to get a shell inside a MySQL database server, and would it matter if there was a tty? You probably don't want `docker exec` at all just to interact with your database.) – David Maze Jan 15 '23 at 14:15

1 Answers1

2

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.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35