gpt4 book ai didi

bash - 如何在 docker 镜像的新容器中运行 bash?

转载 作者:行者123 更新时间:2023-11-29 08:49:28 25 4
gpt4 key购买 nike

我能够在从 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

但是,我无法在从此镜像创建的容器中运行 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 和标准输入:

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

或者为了防止上面的容器被丢弃,在没有 --rm 的情况下运行它.

或者进入一个正在运行的容器,使用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 .

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

所以在前面的两个命令中,如果你运行 bash作为 CMD , 以及默认的 ENTRYPOINT被使用,那么容器将使用

运行
/bin/sh -c bash

如果指定 --entrypoint bash , 然后它运行

bash <command>

在哪里<command>CMD在图像中指定(如果指定)。

关于bash - 如何在 docker 镜像的新容器中运行 bash?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43308319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com