我怎样才能运行一个泊坞窗图像的新容器bash?
我能够在从docker / whalesay图像创build的容器中运行任意的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
但是,我无法在从此映像创build的容器中运行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 run
没有附加一个tty,只能调用bash
,那么bash就没有任何事情要做,它就会退出。 这是因为默认情况下,容器是非交互式的,而以非交互模式运行的shell需要脚本运行。 没有,它会退出。
您可以简单地附加一个tty和标准input。
docker run -it ...
或者,如果您已经有一个正在运行的容器,并且想要使用shell进行input,请使用exec
代替:
docker exec -it <container-name-or-id> bash
在你问的评论中
你知道这和
docker run -it --entrypoint bash docker/whalesay
什么docker run -it --entrypoint bash docker/whalesay
么?
在上面的两个命令中,您将指定bash
作为CMD
。 在这个命令中,你指定bash
作为ENTRYPOINT
。
每个容器都使用ENTRYPOINT
和CMD
的组合运行。 如果您(或图像)没有指定入口ENTRYPOINT
,则默认入口点是/bin/sh -c
。
所以在前两个命令中,如果运行bash
作为CMD
,并且使用了默认的ENTRYPOINT
,那么容器将会运行
/bin/sh -c bash
如果你指定--entrypoint bash
,那么它会运行
bash <command>
其中<command>
是图像中指定的CMD
(如果指定了任何值)。