为什么启动交互式 Docker 容器时必须指定 -i,而不需要 -t?

huangapple go评论50阅读模式
英文:

Why do we have to specify -i when starting an interactive docker container but not the -t?

问题

我在教育中使用Docker,我们使用-i标志启动Linux镜像的容器。为什么在启动容器时不需要指定-t标志,但在使用run命令创建新容器时需要同时指定-i-t标志呢?

比如:

docker start -i [容器名称]

docker run -it ...(其余命令)

在创建容器时,是否容器会永久具有伪终端(pseudo-tty),而如果未指定-t则永久不会具有伪终端?

英文:

I use docker as part of my education and we start containers with Linux images with the -i flag. Why do I not have to specify the -t flag when starting a container, but I have to specify both -i and -t when creating a new container with the run command?

as in:

docker start -i [container name]

vs

docker run -it ... (rest of the command)

Does a container just permanently have that pseudo-tty when you create it? And will it permanently not have one if the -t is not specified?

答案1

得分: 1

为什么在启动容器时不必指定-t标志,但在使用run命令创建新容器时必须同时指定-i-t

首先,我认为在创建容器时不必指定-i-i标志启动容器以交互模式运行,但如果您不需要与容器交互,那么不使用-i标志是完全可以的。您可能希望在后台中运行容器中的Web服务器,在这种情况下,您不会使用-i

如果没有指定-t,那么就会永久不会有一个吗?

正确。您可以通过尝试运行与终端连接时具有特殊行为的程序来查看这种差异:

$ docker run -it ubuntu bash
root@b4c828502868:/# 

但是,如果您在没有终端的情况下启动它,它不会显示提示符。

$ docker run -i ubuntu bash
 

您仍然可以输入命令(即使没有-t,它仍然具有标准输入、标准输出和标准错误输出),但它不会像正常终端一样工作。

英文:

>Why do I not have to specify the -t flag when starting a container, but I have to specify both -i and -t when creating a new container with the run command?

First, I would argue that you don't have to specify -i when creating a container. The -i flag starts the container in interactive mode, but if you don't need to interact with the container, then not having the -i flag is perfectly fine. You might want to start a web server in a container in the background, and in that case you wouldn't use -i.

>And will it permanently not have one if the -t is not specified?

Correct. You can see this difference if you try running a program which has special behavior when connected to a tty:

$ docker run -it ubuntu bash
root@b4c828502868:/# 

However, if you start it without a tty, it won't show you a prompt.

$ docker run -i ubuntu bash
 

You can still type commands (it still has stdin, stdout, and stderr, even without -t) but it won't act like a normal tty.

huangapple
  • 本文由 发表于 2023年6月19日 07:08:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定