说实话,我一直对docker exec -it …,docker exec -i …和docker exec -t …感到困惑,所以我决定做一个测试:
> 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
它正常工作.
> docker exec -i …:
# docker exec -i 115c89122e72 bash
^C
命令挂起,我必须使用Ctl c来中断它.
> 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,一个连接用户的“终端”与stdin和stdout的pseudo terminal. (见container/container.go
)
如果你做回声,只需要-t.
但是对于输入输入的交互式会话,您需要-i.
由于-i保持stdin打开,因此它也用于将输入管道输送到分离的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 exec来说,它是由docker 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 apts
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 thedevpts
from the host in to the containers.
注(2017年第4季度):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回来了“不是tty”)