Confused about Docker -t option to Allocate a pseu

2019-01-12 17:35发布

问题:

What exactly does this option do? I've been reading a lot on TTY and am still confused. I played around with not having the -t and just -i and it seems like programs that expect user input throw an error without the -t. Why is it important for pseudo-TTY to be enabled?

回答1:

The "-t" option goes to how Unix/Linux handles terminal access. In the past, a terminal was a hardline connection, later a modem based connection. These had physical device drivers (they were real pieces of equipment). Once generalized networks came into use, a pseudo-terminal driver was developed. This is because it creates a separation between understanding what terminal capabilities can be used without the need to write it into your program directly (read man pages on stty, curses).

So, with that as background, run a container with no options and by default you have a stdout stream (so docker run | <cmd> works); run with "-i", and you get stdin stream added (so <cmd> | docker run -i works); use "-t", usually in the combination "-it" and you have a terminal driver added, which if you are interacting with the process is likely what you want. It basically makes the container start look like a terminal connection session.



回答2:

The "-t" argument is NOT documented well, or mentioned by many people often, according to a Google search.

It doesn't even show up when you display a list of (what should be) all docker client arguments by typing "docker" at the Bash prompt (with the latest version of 1.8.1).

In fact, if you try to get specific help about this argument by typing "docker -t --help" if gives this amazingly vague reply:

"flag provided but not defined: -t"

So, you can't be blamed for being confused about this argument!

There is a mention in the Docker online documention which says it is to "Allocate a pseudo-tty" and is often used with -i:

https://docs.docker.com/reference/run/

I saw it used in the documentation for the terrific jwilder/nginx-proxy docker container in the following way:

docker run -d -p 80:80 --name nginx -v /tmp/nginx:/etc/nginx/conf.d -t nginx

In this case, what it does is send the output to the 'virtual' tty (Bash command prompt/terminal) within this docker container. You can then see this output by running the docker command "docker logs CONTAINER" where CONTAINER is the first couple of characters of this container's ID. This CONTAINER ID can be found by typing "docker ps -a"

I've seen this "-t" argument mentioned briefly in the following link, where it says, "The -t and -i flags allocate a pseudo-tty and keep stdin open even if not attached. This will allow you to use the container like a traditional VM as long as the bash prompt is running."

https://coreos.com/os/docs/latest/getting-started-with-docker.html

I hope this helps! I'm not sure why this isn't documented or used much. Maybe it's experimental and will be implemented as a documented feature in upcoming versions.



回答3:

What I know about the -t is the following:

docker exec -ti CONTAINER bash - allows me to "login" in the container. It feels like ssh-ing (it's not).

But the trouble was when I wanted to restore a database.

Usually I dodocker exec -ti mysql.5.7 mysql - Here I execute the mysql command in the container and get an interactive terminal.

I added <dump.sql to the previous command so I can restore a db. But it failed with cannot enable tty mode on non tty input.

Removing the -t helped. Still don't understand why:

docker exec -i mysql.5.7 mysql < dump.sql

The last one works. Hope this helps people.



回答4:

The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin, creating an interactive bash shell in the container.

--interactive, -i   false   Keep STDIN open even if not attached

--tty, -t   false   Allocate a pseudo-TTY

https://docs.docker.com/engine/reference/commandline/run/



标签: docker tty pty