什么时候在Docker容器中使用`--interactive`而没有`--tty`? [英] When would I use `--interactive` without `--tty` in a Docker container?

查看:427
本文介绍了什么时候在Docker容器中使用`--interactive`而没有`--tty`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些谷歌搜索,没有运气找到一个案例,我会运行 docker run -i some_image 而不是 docker run -it some_image



如果我运行 docker run -i -name sample some_image bash ,容器在前台运行,但我不能与我进行的shell进行交互。我甚至不能用CTRL + C来阻止它。然而,我可以弹出另一个shell,并运行 docker exec -it sample bash 并访问该容器。



如果我运行 docker run -i -d --name sample some_image bash ,容器立即退出。我可以使用 docker start sample 重新启动它,然后它保持起来,所以我可以运行 docker exec -it sample bash 并且再次与它进行交互。



然而,在所有这些情况下,我最终最终使用 -it 进行交互与我的容器在什么世界上我不需要 -t 标志?



干杯

解决方案

由于 -i 保持STDIN打开即使不附加,也允许组合(管道)。 >
例如:

  docker运行ubuntu printfline1\\\
line2\\\
| docker run -i ubuntu grep line2 |码头运行-i ubuntu sed's / line2 / line3 / g'

(来源: 问题14221



或者:

  $ echo hello | docker run -i busybox cat 
hello

(来源:issue 12401


现在想象一下,这不是在键盘前面,而是在一个脚本中使用,你可以通过一个比shell更好的方式来写入进程stdin | 示例 integration-cli / docker_cli_attach_test.go



I did some googling and have had no luck finding a case where I'd run docker run -i some_image rather than docker run -it some_image.

If I run docker run -i --name sample some_image bash, the container runs in the foreground, but I can't interact with it from the shell I'm in. I can't even stop it with CTRL+C. I can, however, pop open another shell and run docker exec -it sample bash and gain access to the container.

If I run docker run -i -d --name sample some_image bash, the container immediately exits. I can restart it with docker start sample and then it stays up, so I can run docker exec -it sample bash and interact with it again.

However, in all these cases, I ultimately end up using -it to interact with my containers. In what world would I not need the -t flag?

Cheers

解决方案

Since -i keeps STDIN open even if not attached, it allows for composition (piping).
For example:

docker run ubuntu printf "line1\nline2\n" | docker run -i ubuntu grep line2 | docker run -i ubuntu sed 's/line2/line3/g'

(Source: issue 14221)

Or:

$ echo hello | docker run -i busybox cat
  hello

(Source: issue 12401)

Now imagine this not in front of a keyboard and being used in a script where you can actually write to the processes stdin through something better than a shell |: example integration-cli/docker_cli_attach_test.go

这篇关于什么时候在Docker容器中使用`--interactive`而没有`--tty`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆