-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 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年第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”)