如何在 docker 镜像的新容器中运行 bash?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43308319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run bash in a new container of a docker image?
提问by Lone Learner
I am able to run arbitrary shell commands in a container created from docker/whalesay image.
我能够在从 docker/whalesay 映像创建的容器中运行任意 shell 命令。
$ docker run docker/whalesay ls -l
total 56
-rw-r--r-- 1 root root 931 May 25 2015 ChangeLog
-rw-r--r-- 1 root root 385 May 25 2015 INSTALL
-rw-r--r-- 1 root root 1116 May 25 2015 LICENSE
-rw-r--r-- 1 root root 445 May 25 2015 MANIFEST
-rw-r--r-- 1 root root 1610 May 25 2015 README
-rw-r--r-- 1 root root 879 May 25 2015 Wrap.pm.diff
drwxr-xr-x 2 root root 4096 May 25 2015 cows
-rwxr-xr-x 1 root root 4129 May 25 2015 cowsay
-rw-r--r-- 1 root root 4690 May 25 2015 cowsay.1
-rw-r--r-- 1 root root 54 May 25 2015 install.pl
-rwxr-xr-x 1 root root 2046 May 25 2015 install.sh
-rw-r--r-- 1 root root 631 May 25 2015 pgp_public_key.txt
$ docker run docker/whalesay lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.2 LTS
Release: 14.04
Codename: trusty
However, I am unable to run a shell in a container created from this image.
但是,我无法在从此映像创建的容器中运行 shell。
$ docker run docker/whalesay bash
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ce600cc9904 docker/whalesay "bash" 5 seconds ago Exited (0) 3 seconds ago loving_mayer
Why did it not work? How can I make it work?
为什么它不起作用?我怎样才能让它工作?
回答by Dan Lowe
If you docker run
without attaching a tty, and only call bash
, then bash finds nothing to do, and it exits. That's because by default, a container is non-interactive, and a shell that runs in non-interactive mode expects a script to run. Absent that, it will exit.
如果您docker run
没有附加 tty,而只调用bash
,那么 bash 将无事可做,然后退出。这是因为默认情况下,容器是非交互式的,并且以非交互式模式运行的 shell 需要运行脚本。如果没有,它将退出。
To run a new container, you can simply attach a tty and standard input:
要运行一个新容器,您只需附加一个 tty 和标准输入:
docker run -it --entrypoint bash <image-name-or-id>
Or to enter a running container, use exec
instead:
或者要进入正在运行的容器,请exec
改用:
docker exec -it <container-name-or-id> bash
In comments you asked
在你问的评论中
Do you know what is the difference between this and
docker run -it --entrypoint bash docker/whalesay
?
你知道这和 有
docker run -it --entrypoint bash docker/whalesay
什么区别吗?
In the two commands above, you are specifying bash
as the CMD
. In this command, you are specifying bash
as the ENTRYPOINT
.
在上面的两个命令中,您指定bash
为CMD
. 在此命令中,您指定bash
为ENTRYPOINT
.
Every container is run using a combination of ENTRYPOINT
and CMD
. If you (or the image) does not specify ENTRYPOINT
, the default entrypoint is /bin/sh -c
.
每个容器使用的组合运行ENTRYPOINT
和CMD
。如果您(或图像)未指定ENTRYPOINT
,则默认入口点为/bin/sh -c
。
So in the earlier two commands, if you run bash
as the CMD
, and the default ENTRYPOINT
is used, then the container will be run using
所以在前面的两个命令中,如果你bash
作为运行CMD
,并且使用默认值ENTRYPOINT
,那么容器将使用
/bin/sh -c bash
If you specify --entrypoint bash
, then instead it runs
如果您指定--entrypoint bash
,则它会运行
bash <command>
Where <command>
is the CMD
specified in the image (if any is specified).
凡<command>
在CMD
图像中指定的(如果指定的话)。