,所以我决定做一个测试: docker exec -it .-6ren">
gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:24:47 28 4
gpt4 key购买 nike

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

  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

    正常工作。

  2. docker exec -i ...:

    # docker exec -i 115c89122e72 bash
    ^C

    命令挂起,我必须使用 Ctl + c 来中断它。

  3. docker exec -t ...:

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

    成功进入容器,但执行第一个命令时挂起。

因此,使用 docker exec -i ...docker exec -t ... 命令似乎没有意义。谁能详细说明为什么 docker exec 命令存在 -i-t 选项?

最佳答案

-i, --interactive 即使没有附加 STDIN 也会保持打开状态,如果你想键入任何命令,你需要它。

-t, --tty 分配一个伪TTY,一个pseudo terminal它将用户的“终端”与标准输入和标准输出连接起来。 (见 container/container.go)

如果做回显,只需要-t
但是对于输入输入的交互式 session ,您需要 -i.

由于 -i 保持标准输入处于打开状态,它也用于将输入通过管道传输到分离的 docker 容器。即使使用 -d (分离)也可以。
见“When would I use --interactive without --tty in a Docker container?”:

$ 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?

docker execdocker run 设置的。

但是,关于 docker exec ,当前存在一个问题 (issue 8755: Docker tty is not a tty with docker exec

unfortunately your discovery only amounts to a difference between the behaviour of tty in centos6 vs ubuntu:14.04. 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.

the actual bug we're dealing with is that certain standard libraries assume that the symlinks in /proc/self/fds/ must be valid symlinks

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.
One options to fix this would be allocate and bind mount the devpts from the host in to the containers.

注(2017 年第四季度):this should been fixed by now (docker 17.06-ce) .
PR 33007 .

该 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

(在 17.06 之前,tty 返回“not a tty”)

关于linux - "-i"命令的 "-t"和 "docker exec"选项的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35550694/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com