简体   繁体   English

docker run --tty 和 --interactive 开关之间的区别

[英]Difference between the docker run --tty and --interactive switches

I feel there is a subtle difference between the --tty and --interactive switches of the docker run command, that I don't grasp:我觉得--tty docker run命令的--tty--interactive开关之间存在细微差别,我不明白:

  • --interactive , -i : Keep STDIN open even if not attached --interactive , -i : 即使没有连接,也保持 STDIN 打开
  • --tty , -t : Allocate a pseudo-TTY --tty , -t : 分配一个伪 TTY

So I decided to run some tests.所以我决定进行一些测试。
First I created a basic Python script, which continuously prints a string.首先,我创建了一个基本的 Python 脚本,它连续打印一个字符串。
Then I created a basic docker image, which will run this script when a container is started.然后我创建了一个基本的 docker 镜像,它将在容器启动时运行这个脚本。

my_script.py my_script.py

import time

while True:
    time.sleep(1)
    print('still running...')

Dockerfile文件

FROM python:3.8.1-buster
COPY my_script.py /
CMD [ "python3", "/my_script.py"]

Built using command:使用命令构建:

docker build --tag pytest .

Test 1测试 1
I run docker run --name pytest1 -i pytest , to test the interactive behaviour of the container.我运行docker run --name pytest1 -i pytest来测试容器的交互行为。 Nothing is printed to the console, but when I press Control+C the python script is interrupted and the container stops running.控制台没有打印任何内容,但是当我按Control+C ,python 脚本被中断并且容器停止运行。

This confirms my thinking that stdin was open on the container and my keyboard input entered the container.这证实了我认为标准输入在容器上打开并且我的键盘输入进入了容器的想法。

Test 2测试 2
I run docker run --name pytest1 -t pytest , to test the pseudo-tty behaviour of the container.我运行docker run --name pytest1 -t pytest来测试容器的伪 tty 行为。 It repeatedly prints still running... to the console, ánd when I press Control+C the python script is interrupted and the container stops running.它反复打印still running...到控制台,当我按Control+C ,python 脚本被中断并且容器停止运行。

Test 3测试 3
I run docker run --name pytest1 -it pytest , to test the combined behaviour.我运行docker run --name pytest1 -it pytest来测试组合行为。 The behaviour is the same as in Test 2.行为与测试 2 中的相同。

Questions问题

  • What are the nuances I'm missing here?我在这里遗漏了什么细微差别?
  • Why would one use the combined -it switches, as you often see, if there is no benefit to the -t switch?如果-t开关没有任何好处,为什么要使用组合的-it开关,正如您经常看到的那样?
  • Does the --tty switch just keeps bóth stdin and stdout open? --tty开关是否只保持 stdin 和 stdout 打开?

-t option is needed if you want to interact with a shell like /bin/sh for instance. -t ,如果您想与 /bin/sh 之类的 shell 进行交互,则需要-t选项。 The shell works by controlling tty. shell 通过控制 tty 来工作。 No tty available, no shell.没有可用的 tty,没有外壳。

we use -i in combination with -t to be able to write commands to the shell we opened我们将-i-t结合使用,以便能够向我们打开的 shell 写入命令

few tests you could reproduce to understand:您可以重现以理解的一些测试:

docker run alpine /bin/sh : the container exits. docker run alpine /bin/sh :容器退出。 shell needs to wait for stdin shell 需要等待 stdin

docker run -i alpine /bin/sh : the container stays, but the shell won't start. docker run -i alpine /bin/sh :容器保持不变,但外壳不会启动。 we cannot type commands我们不能输入命令

docker run -t alpine /bin/sh : shell starts, but we are stuck, the keys we press are not interpreted docker run -t alpine /bin/sh : shell 启动了,但是我们卡住了,我们按下的键没有被解释

docker run -it alpine /bin/sh : yeah our shell is working docker run -it alpine /bin/sh :是的,我们的 shell 正在运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM