如何在docker映像的新容器中运行bash? [英] How can I run bash in a new container of a docker image?

查看:113
本文介绍了如何在docker映像的新容器中运行bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在从docker/whalesay映像创建的容器中运行任意shell命令.

I am able to run arbitrary shell commands in a container created from docker/whalesay image.

$ 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.

$ 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?

推荐答案

如果您在不附加tty的情况下 docker run ,并且仅调用 bash ,则bash不会找到任何内容做,它退出.这是因为默认情况下,容器是非交互式的,并且以非交互式模式运行的shell希望脚本能够运行.如果没有,它将退出.

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.

要运行一个一次性的新容器,您只需附加一个tty和标准输入即可:

To run a disposable new container, you can simply attach a tty and standard input:

docker run --rm -it --entrypoint bash <image-name-or-id>

或者为防止处置上述容器,请在没有-rm 的情况下运行它.

Or to prevent the above container from being disposed, run it without --rm.

或者要输入正在运行的容器,请改用 exec :

Or to enter a running container, use exec instead:

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


在您询问的评论中


In comments you asked

您知道这和 docker run -it --entrypoint bash docker/whalesay 有什么区别吗?

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

In the two commands above, you are specifying bash as the CMD. In this command, you are specifying bash as the ENTRYPOINT.

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

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.

因此,在前面的两个命令中,如果以 CMD 身份运行 bash ,并且使用默认的 ENTRYPOINT ,则容器将使用

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

/bin/sh -c bash

如果您指定-entrypoint bash ,则它将运行

If you specify --entrypoint bash, then instead it runs

bash <command>

其中< command> 是图像中指定的 CMD (如果已指定).

Where <command> is the CMD specified in the image (if any is specified).

这篇关于如何在docker映像的新容器中运行bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆