【问题标题】:why would we want to use both --detach switch with --interactive and --tty in docker?为什么我们要在 docker 中同时使用 --detach 开关和 --interactive 和 --tty ?
【发布时间】:2021-08-13 19:31:07
【问题描述】:

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

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

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

【问题讨论】:

  • 这当然不常见,但也许您计划将来运行docker attach 以附加到需要 tty 的交互式程序。
  • 可能,能够在不中断整个容器的情况下退出容器外壳

标签: docker detach


【解决方案1】:

对于那个特定的命令,它没有意义,因为 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    相关资源
    最近更新 更多