2017-04-09 292 views
0

我能够在从docker/whalesay图像创建的容器中运行任意shell命令。如何在Docker镜像的新容器中运行bash?

$ 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 

但是,我无法运行在此图像中创建一个容器外壳。

$ 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 

为什么它不起作用?我怎样才能使它工作?

+0

什么'ps'后你跑'bash'输出? – Sundeep

+0

@Sundeep在我的问题中增加了'ps'的输出。 –

+0

当你执行* docker exec -it 7ce600cc9904/bin/bash *时会发生什么? – SilentMonk

回答

3

如果你docker run没有附加一个tty,只有调用bash,然后bash找不到任何事情,它退出。这是因为默认情况下,容器是非交互式的,并且以非交互模式运行的shell期望脚本运行。如果没有,它会退出。

你可以简单地附上一个tty和标准输入。

docker run -it ... 

或者,如果你有一个正在运行的容器已经和希望与外壳进入它,使用exec代替:

docker exec -it <container-name-or-id> bash 

在评论你问

Do you know what is the difference between this and docker run -it --entrypoint bash docker/whalesay ?

在上面的两个命令,您将bash指定为CMD。在此命令中,您指定bash作为ENTRYPOINT

每个容器使用的ENTRYPOINTCMD组合运行。如果您(或图片)未指定ENTRYPOINT,则默认入口点为/bin/sh -c

所以在前面的两个命令,如果您运行bashCMD,默认ENTRYPOINT使用,则容器将使用

/bin/sh -c bash 

如果指定--entrypoint bash运行,则相反,它运行

bash <command> 

<command>是图像中指定的CMD(如果指定的话)。

+0

感谢。 “码头运行 - 码头工人/ whalesay bash”的作品。你知道这和'docker run -it -entrypoint bash docker/whalesay'有什么区别吗? –

+0

@LoneLearner查看我的更新回答。 –

+0

谢谢!说得通。 'docker run -it --entrypoint ls docker/whalesay -l'确实执行'ls -l'并打印长列表格式的目录列表。 –

相关问题