如何进入一个ubuntudocker容器的bash?

How to enter bash of an ubuntu docker container?

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

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

ubuntu容器直接退出。我怎样才能输入bash?

使用 -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 Keep STDIN open even if not attached

-t, --tty=false Allocate a pseudo-TTY

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