如何在docker映像的新容器中运行bash?
我能够在从docker/whalesay图像创建的容器中运行任意shell命令如何在docker映像的新容器中运行bash?,bash,docker,Bash,Docker,我能够在从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
$ 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
但是,我无法在从此图像创建的容器中运行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
为什么不起作用?如何使其工作?如果您运行
docker
而不附加tty,并且只调用bash
,则bash找不到任何操作,它将退出。这是因为默认情况下,容器是非交互的,而在非交互模式下运行的shell需要运行脚本。否则,它将退出
要运行一次性新容器,只需附加tty和标准输入:
docker run --rm -it --entrypoint bash <image-name-or-id>
在你询问的评论中 你知道这和docker run-it-entrypoint bash-docker/whalesay有什么区别吗 在上面的两个命令中,您将
bash
指定为CMD
。在这个命令中,您将bash
指定为ENTRYPOINT
每个容器都使用ENTRYPOINT
和CMD
的组合运行。如果您(或图像)未指定入口点
,则默认入口点为/bin/sh-c
因此,在前面的两个命令中,如果您以CMD
的形式运行bash
,并且使用默认的ENTRYPOINT
,那么容器将使用
/bin/sh -c bash
如果指定--entrypoint bash
,则它将运行
bash <command>
bash
其中
是图像中指定的CMD
(如果指定了任何)。执行docker exec时会发生什么情况-it 7ce600cc9904/bin/bash?@SilentMonk出现此错误时失败:来自守护进程的错误响应:Container 7CE600CC99046318CC8FA073014942D99437B4FF0D6927031C8A839E8A8803E未运行
谢谢<代码>docker运行-it docker/whalesay bash工作。你知道这和docker run-it-entrypoint bash docker/whalesay有什么区别吗?谢谢!有道理docker run-it--entrypoint ls docker/whalesay-l
确实执行ls-l
并以长列表格式打印目录列表。
bash <command>