为什么我们要在 docker 中将 --detach 开关与 --interactive 和 --tty 一起使用?

why would we want to use both --detach switch with --interactive and --tty in docker?

我正在阅读 docker documentations,我看到了这个命令:

$ docker run -d \
     -it \
     --name devtest \
     --mount type=bind,source="$(pwd)"/target,target=/app,readonly \
     nginx:latest

据我所知,使用-d--detach切换运行当前终端仿真器之外的命令,return控制终端返回用户。而使用 --tty -t--interactive -i 则完全相反。为什么会有人想在命令中使用它们?

对于那个特定的命令,它没有意义,因为 nginx 没有交互式组件。但一般来说,它允许您稍后使用 docker attach 附加到容器。例如

$ docker run --name test-no-input -d busybox /bin/sh
92c0447e0c19de090847b7a36657d3713e3795b72e413576e25ab2ce4074d64b

$ docker attach test-no-input
You cannot attach to a stopped container, start it first

$ docker run --name test-input -dit busybox /bin/sh
57e4adcc14878261f64d10eb7839b35d5fa65c841bbcb3cd81b6bf5b8fe9d184

$ docker attach test-input
/ # echo hello from the container
hello from the container
/ # exit

第一个容器停止,因为它是 运行 a shell,并且标准输入上没有输入(没有 -i)。 shell 在完成读取输入时退出(例如 shell 脚本的结尾)。