简体   繁体   English

“docker exec”命令的“-i”和“-t”选项的用途是什么?

[英]What is the purpose of the “-i” and “-t” options for the “docker exec” command?

To be honest, I have always been confused about docker exec -it … , docker exec -i … and docker exec -t … , so I decide to do a test: 说实话,我一直对docker exec -it …docker exec -i …docker exec -t …感到困惑,所以我决定做一个测试:

  1. docker exec -it … : docker exec -it …

     # docker exec -it 115c89122e72 bash root@115c89122e72:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var 

    It works normally. 它正常工作。

  2. docker exec -i … : docker exec -i …

     # docker exec -i 115c89122e72 bash ^C 

    The command hangs and I have to use Ctl + c to interrupt it. 该命令挂起,我必须使用Ctl + c来中断它。

  3. docker exec -t … : docker exec -t …

     # docker exec -t 115c89122e72 bash root@115c89122e72:/# ls ^C 

    It enters the container successfully but hangs on executing the first command. 它成功进入容器,但在执行第一个命令时挂起。

So it seems there is no point in having the docker exec -i … and docker exec -t … commands. 所以似乎没有必要使用docker exec -i …docker exec -t …命令。 Could anyone elaborate on why there exist -i and -t options for the docker exec command? 任何人都可以详细说明为什么docker exec命令存在-i-t选项?

-i , --interactive keeps STDIN open even if not attached, which you need if you want to type any command at all. -i , - --interactive使STDIN保持打开即使没有附加,如果你想输入任何命令,你需要它。

-t , --tty Allocates a pseudo-TTY, a pseudo terminal which connects a user's "terminal" with stdin and stdout. -t , - --tty分配一个伪TTY,一个将用户的“终端”与stdin和stdout连接起来的伪终端 (See container/container.go ) (见container/container.go

If you do an echo, only -t is needed. 如果你做回声,只需要-t
But for an interactive session where you enter inputs, you need -i . 但是对于输入输入的交互式会话,您需要-i

Since -i keeps stdin open, it is also used in order to pipe input to a detached docker container. 由于-i保持stdin打开,因此它也用于将输入管道输送到分离的docker容器。 That would work even with -d (detach). 即使使用-d (分离)也可以。
See " When would I use --interactive without --tty in a Docker container? ": 请参阅“ --tty在Docker容器中使用--interactive而没有--tty ”:

$ echo hello | docker run -i busybox cat
  hello

-i keeps STDIN open even if not attached, what is the status of STDOUT in this case? 即使没有连接, -i也会保持STDIN打开,在这种情况下STDOUT的状态是什么?

It is, for docker exec , the one set by docker run . 对于docker exec ,它是由docker run设置的。

But, regarding docker exec , there is a current issue ( issue 8755: Docker tty is not a tty with docker exec 但是,关于docker exec ,有一个当前的问题( 问题8755:Docker tty与docker exec

unfortunately your discovery only amounts to a difference between the behaviour of tty in centos6 vs ubuntu:14.04. 不幸的是,你的发现只相当于centos6与ubuntu:14.04的tty行为之间的差异。 There is still not a functional tty inside the exec - just do ls -la /proc/self/fd/0 and see that it's a broken link pointing to a pts which doesn't exist. exec内部仍然没有功能性tty - 只需执行ls -la /proc/self/fd/0并看到它是指向不存在的pts的断开链接。

the actual bug we're dealing with is that certain standard libraries assume that the symlinks in /proc/self/fds/ must be valid symlinks 我们正在处理的实际错误是某些标准库假定/ proc / self / fds /中的符号链接必须是有效的符号链接

The problem is that the tty is created outside on the host and there is no reference to it in the container like how /dev/console is setup in the primary container. 问题是tty是在主机外部创建的,并且在容器中没有引用它,就像在主容器中设置/dev/console一样。
One options to fix this would be allocate and bind mount the devpts from the host in to the containers. 修复此问题的一个选项是分配和绑定将主机的devpts挂载到容器中。

Note (Q4 2017): this should been fixed by now (docker 17.06-ce) . 注(2017年第4季度): 现在应该已经修好了(码头17.06-ce)
See PR 33007 . PR 33007

That PR now allows (since 17.06): PR现在允许(从17.06开始):

zacharys-pro:dev razic$ docker run --rm -t -d ubuntu bash
83c292c8e2d13d1b1a8b34680f3fb95c2b2b3fef71d4ce2b6e12c954ae50965a

zacharys-pro:dev razic$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
83c292c8e2d1        ubuntu              "bash"              2 seconds ago       Up 1 second                             xenodochial_bardeen

zacharys-pro:dev razic$ docker exec -ti xenodochial_bardeen tty
/dev/pts/1

(before 17.06, tty was returning " not a tty ") (在17.06之前, tty回来了“ not a tty ”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM