Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

terminal - Why run Docker container with -t?

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...

question from:https://stackoverflow.com/questions/65876075/why-run-docker-container-with-t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...