【问题标题】:How to enter bash of an ubuntu docker container?如何输入 ubuntu docker 容器的 bash?
【发布时间】:2015-05-07 02:30:50
【问题描述】:

我想运行一个ubuntu容器并输入bash

[root@localhost backup]# docker run ubuntu bash
[root@localhost backup]#

ubuntu 容器直接退出。如何输入bash

【问题讨论】:

    标签: linux ubuntu docker


    【解决方案1】:

    使用-i-t 选项。

    示例:

    $ docker run -i -t ubuntu /bin/bash
    root@9055316d5ae4:/# echo "Hello Ubuntu"
    Hello Ubuntu
    root@9055316d5ae4:/# exit
    

    见:Docker run Reference

    $ docker run --help | egrep "(-i,|-t,)"

    -i, --interactive=false 即使没有附加 STDIN 也保持打开

    -t, --tty=false 分配一个伪TTY

    更新:之所以能保持容器运行(运行/bin/bash)是因为-i-t 选项(特别是-i) 保持 STDIN 处于打开状态,因此 /bin/bash 不会立即终止,从而终止容器。 -- 你还需要/想要-t 的原因是因为你可能想要一个类似终端的交互式会话,所以t 为你创建了一个新的伪tty。 -- 此外,如果您在不使用-i/-t 选项的情况下查看docker ps -a 的输出,您会看到您的容器以0退出代码 正常终止。

    【讨论】:

    • 如果没有'-i -t',为什么ubuntu容器会退出?加-i -t选项和不加选项能否详细解释一下?
    • 感谢您的解释!但是我还是有点疑惑:-i, --interactive=false Keep STDIN open even if not attachedattached是什么意思?你能解释一下吗?
    • "attached" 表示附加到 tty 或管道。如果您只使用 -i 本身就是这种情况。
    • @JamesMills Keep STDIN open even if not attached,那么,如何附加到docker run 中的容器?如果我想使用 STDIN,我似乎必须使用 -i...
    猜你喜欢
    • 2017-09-11
    • 2020-02-06
    • 2016-06-14
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2018-09-14
    相关资源
    最近更新 更多