本文介绍了为什么存在“-i”和“-t” “docker exec”的选项命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

老实说,我一直对 docker exec -it ... docker exec -i ... 感到困惑和 docker exec -t ... ,所以我决定做一个测试:


  1. docker exec -it ...

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

    正常工作


  2. docker exec -i ...

     #docker exec -i 115c89122e72 bash 
    ^ C

    该命令挂起,我必须使用 + 中断


  3. docker exec -t ...

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

    它成功进入容器,但挂起执行第一个c ommand。


所以,似乎没有必要让$ code> docker exec -i ... docker exec -t ... 命令。任何人都可以详细说明为什么存在 docker exec -i -t / code>命令?

解决方案

-i code> - 交互式保持STDIN打开,即使没有附加,如果你想输入任何命令,你需要的。



-t - tty 分配伪TTY,一个,将用户的终端连接到stdin和stdout。 (请参阅)



如果你做回音,只有 -t 需要。

但是对于输入输入的交互式会话,您需要 -i



由于 -i 保持stdin打开,它也用于将输入管道连接到已分离的Docker容器。即使使用 -d (detach)也可以使用。

请参阅 :

  $ echo hello | docker run -i busybox cat 
hello






对于 docker exec ,由 docker运行



但是,关于,目前有一个问题(


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:

  1. 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 115c89122e72 bash
    ^C
    

    The command hangs and I have to use + to interrupt it.

  3. 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. Could anyone elaborate on why there exist -i and -t options for the docker exec command?

解决方案

-i, --interactive keeps STDIN open even if not attached, which you need if you want to type any command at all.

-t, --tty Allocates a pseudo-TTY, a pseudo terminal which connects a user's "terminal" with stdin and stdout. (See container/container.go)

If you do an echo, only -t is needed.
But for an interactive session where you enter inputs, you need -i.

Since -i keeps stdin open, it is also used in order to pipe input to a detached docker container. That would work even with -d (detach).
See "When would I use --interactive without --tty in a Docker container?":

$ echo hello | docker run -i busybox cat
  hello


It is, for docker exec, the one set by docker run.

But, regarding docker exec, there is a current issue (issue 8755: Docker tty is not a tty with docker exec

这篇关于为什么存在“-i”和“-t” “docker exec”的选项命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 09:21