What does "the input device is not a TTY" exactly mean in "docker run" output?
Late answer, but might help someone
docker run/exec -i will connect the STDIN of the command inside the container to the STDIN of the
docker run/exec itself.
docker run -i alpine catgives you an empty line waiting for input. Type "hello" you get an echo "hello". The container will not exit until you send CTRL+D because the main process
catis waiting for input from the infinite stream that is the terminal input of the
- On the other hand
echo "hello" | docker -i run alpine catwill print "hello" and exit immediately because
catnotices that the input stream has ended and terminates itself.
If you try
docker ps after you exit either of the above, you will not find any running containers. In both cases,
cat itself has terminated, thus docker has terminated the container.
Now for "-t", this tells the main process inside docker that its input is a terminal device.
docker run -t alpine catwill give you an empty line, but if you try to type "hello", you will not get any echo. This is because while
catis connected to a terminal input, this input is not connected to your input. The "hello" that you typed did not reach the input of
catis waiting for input that never arrives.
echo "hello" | docker run -t alpine catwill also give you an empty line and will not exit the container on CTRL-D but you will not get an echo "hello" because you didn't pass
If you send CTRL+C, you get your shell back, but if you try
docker ps now, you see the
cat container still running. This is because
cat is still waiting on an input stream that was never closed. I have not found any useful use for the
-t alone without being combined with
-it together. This tells cat that its input is a terminal and in the same time connect this terminal to the input of
docker run which is a terminal.
docker run/exec will make sure that its own input is in fact a tty before passing it to
cat. This is why you will get a
input device is not a TTY if you try
echo "hello" | docker run -it alpine cat because in this case, the input of
docker run itself is the pipe from the previous echo and not the terminal where
docker run is executed
Finally, why would you need to pass
-i will do the trick of connecting your input to
cat's input? This is because commands treat the input differently if it's a terminal. This is also best illustrated by example
docker run -e MYSQL_ROOT_PASSWORD=123 -i mariadb mysql -uroot -pwill give you a password prompt. If you type the password, the characters are printed visibly.
docker run -i alpine shwill give you an empty line. If you type a command like
lsyou get an output, but you will not get a prompt or colored output.
In the last two cases, you get this behavior because
mysql as well as
shell were not treating the input as a tty and thus did not use tty specific behavior like masking the input or coloring the output.
This answer helped me to wrap my head around:
- by default (without neither
-toptions) a Docker container only sends its output to STDOUT,
-ioption comes connection to STDIN,
-toption pulls in a terminal interface driver, that works on top of STDIN/STDOUT. And when a terminal driver is pulled in, the communication with a container must conform to the terminal interface protocol. Piping a string does not.
A tty indicates you have a terminal, something that would be provided by xterm or one of the many linux command line interfaces. It needs a keyboard and text output interface associated with it. Typical reasons to want this is for color text output support, handling of various key combinations (like the arrow keys), and the ability to move the cursor around the screen.
When you pipe a command into docker like your
echo example shows, that pipe is the input, and that pipe does not have a tty interface, it's just a stream of text. Attempting to create a tty with that will fail as the error message indicates.