1

The Docker Run Reference says that running a container with -t

-t : Allocate a pseudo-tty

But only running it with -i allows the user to interact with the containerized process through the terminal. So I wonder, what is the meaning of "Allocating a pseudo-tty", since even when running without -t, content written to STDOUT by the process will be passed to the terminal (The process will have a pipe as stdout instead of a tty) ?

I read this answer which says that you may run docker run -t to have "Terminal support", such as text coloring etc. Well I already done the following experiment:

// Dockerfile

FROM ubuntu:latest

CMD ["echo", "-e", "\u001b[31mHello World"]

And ran this image with no -t. Since I'm running it from a terminal (docker run will always run from some terminal won't it?) I can see a red "Hello World". So I still don't understand why running with -t alone...

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
YoavKlein
  • 2,005
  • 9
  • 38
  • 1
    Does the second part of [this answer](https://stackoverflow.com/a/43099210/596285) help? – BMitch Jan 24 '21 at 21:33
  • Try `docker run -i --rm ubuntu bash` and notice the lack of a prompt. Install and try to use vim without the `-t` in that shell. Press the up arrow to go back to the previous command. – BMitch Jan 25 '21 at 15:31
  • That's interesting thank you! I can see the need to use `-t` when you do use `-i`, my question was more about why using `-t` when you're not using `-i` – YoavKlein Jan 25 '21 at 16:45
  • some commands check for a tty when creating their output. E.g. there's no technical reason bash can't output a prompt without a tty, but they check and change the behavior of the app based on whether it exists. – BMitch Jan 25 '21 at 18:46

1 Answers1

2

Your test shows that color codes still work, but what it's missing is that without -t many programs will stop trying to send them.

Many programs check isatty(3) in C or [ -t 0 ] from shell scripts to see if they're connected to a TTY. If so they'll enable interactive features like colors and line editing to make the experience more pleasant.

You can witness this with shells like Bash and text editors like Vim. Programs like ls, grep and git have "auto" color modes that only use ANSI color codes interactively. If you leave out -t they'll revert to plaintext.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578