Docker interactive tty attach detach 选项的真正含义?

Docker interactive tty attach detach options real meaning?

我是运行一个容器docker run --name cont -dt img。据我了解,这为容器提供了一个 tty,但不会将我的主机终端的 stdout、stderr 和 stdin 附加到容器,该容器也在后台 运行。

现在我认为如果我这样做 docker attach cont(docker 文档说它将 stdio 附加到 运行 容器,attach)我会获得相同的效果( by 运行 first docker logs cont to see the stdout before the prompt) as 运行 docker run --name cont -it img (display stdout/sterr allocate a tty and connect the stdin to the container)。但是我的主机终端连接但只是挂起并且必须使用 ctrl+c 来分离(ctrl+p+q 将不起作用)。

我想了解为什么 attach 没有像我预期的那样工作?我的意思是它背后的真正逻辑以及如果可能的话你是如何理解这一点的,我知道 运行 cont 带有选项 -dit 并重新附加有效。如果有人有提示或可以指出一些阅读资源来理解这一点,那将会有很大帮助。谢谢!

Dockerfile

FROM ubuntu:latest

RUN apt update -y
COPY . /tmp
WORKDIR /tmp
CMD ["/tmp/app.sh"]

app.sh

#!/bin/sh
echo "Enter your name: "
read name
echo "Hello ${name}!"

如果没有 -i,您还没有将标准输入连接到容器。您仍然会看到使用 tty 的任何格式化输出,但它只是 stdout/stderr。根据您的实验,分离信号 (cont-P cont-Q) 需要连接 stdin 才能进行处理。有关 -it 标志的更多信息,请参阅