如何在已经存在的Docker容器上运行命令?

本文翻译自:How do I run a command on an already existing Docker container?

I created a container with -d so it's not interactive. 我使用-d创建了一个容器,因此它不是交互式的。

docker run -d shykes/pybuilder bin/bash

I see that the container has exited: 我看到容器已退出:

CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                      PORTS               NAMES
d6c45e8cc5f0        shykes/pybuilder:latest   "bin/bash"          41 minutes ago      Exited (0) 2 seconds ago                        clever_bardeen

Now I would like to run occasional commands on the machine and exit. 现在,我想在计算机上偶尔运行命令并退出。 Just to get the response. 只是为了得到回应。

I tried to start the machine. 我试图启动机器。 I tried attaching. 我尝试附加。 I thought I could call run with a container, but that does not seem to be allowed. 我以为可以用容器调用run ,但这似乎是不允许的。 Using start just seems to run and then exist quickly. 使用start似乎可以运行,然后迅速存在。

I'd like to get back into interactive mode after exiting. 我想退出后回到互动模式。

I tried: 我试过了:

docker attach d6c45e8cc5f0

But I get: 但是我得到:

2014/10/01 22:33:34 You cannot attach to a stopped container, start it first

But if I start it, it exits anyway. 但是,如果我启动它,它仍然会退出。 Catch 22. I can't win. 赶上22.我赢不了。


#1楼

参考:https://stackoom.com/question/1ljle/如何在已经存在的Docker容器上运行命令


#2楼

Assuming the image is using the default entrypoint /bin/sh -c , running /bin/bash will exit immediately in daemon mode ( -d ). 假设映像使用默认入口点/bin/sh -c ,则运行/bin/bash将在守护程序模式( -d )下立即退出。 If you want this container to run an interactive shell, use -it instead of -d . 如果要此容器运行交互式外壳,请使用-it而不是-d If you want to execute arbitrary commands in a container usually executing another process, you might want to try nsenter or nsinit . 如果要在通常执行另一个进程的容器中执行任意命令,则可能要尝试使用nsenternsinit Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for the details. 有关详细信息, 参见https://blog.codecentric.de/en/2014/07/enter-docker-container/


#3楼

Your container will exit as the command you gave it will end. 容器将退出,因为您给出的命令将结束。 Use the following options to keep it live: 使用以下选项可以使其保持活动状态:

  • -i Keep STDIN open even if not attached. -i即使没有连接,也请保持STDIN处于打开状态。
  • -t Allocate a pseudo-TTY. -t分配伪TTY。

So your new run command is: 因此,您的新run命令是:

docker run -it -d shykes/pybuilder bin/bash

If you would like to attach to an already running container: 如果要附加到已经运行的容器:

docker exec -it CONTAINER_ID /bin/bash

In these examples /bin/bash is used as the command. 在这些示例中, /bin/bash用作命令。


#4楼

In October 2014 the Docker team introduced docker exec command : https://docs.docker.com/engine/reference/commandline/exec/ 在2014年10月, Docker团队引入了docker exec命令https : //docs.docker.com/engine/reference/commandline/exec/

So now you can run any command in a running container just knowing its ID (or name): 因此,现在您可以在运行容器中运行任何命令,只需知道其ID(或名称)即可:

docker exec -it <container_id_or_name> echo "Hello from container!"

Note that exec command works only on already running container. 请注意, exec命令仅适用于已经运行的容器。 If the container is currently stopped, you need to first run it with the following command: 如果容器当前已停止,则需要首先使用以下命令运行它:

docker run -it -d shykes/pybuilder /bin/bash

The most important thing here is the -d option, which stands for detached . 这里最重要的是-d选项,代表detached It means that the command you initially provided to the container ( /bin/bash ) will be run in the background and the container will not stop immediately . 这意味着您最初提供给容器的命令( /bin/bash )将在后台运行,并且容器不会立即停止


#5楼

Some of the answers here are misleading because they concern containers that are running, not stopped. 此处的某些答案具有误导性,因为它们与正在运行而不是停止的容器有关。

Sven Dowideit explained on the Docker forum that containers are bound to their process (and Docker can't change the process of a stopped container, seemingly due at least to its internal structure: https://github.com/docker/docker/issues/1437 ). Sven Dowideit在Docker论坛上解释说,容器已绑定到其进程(而Docker似乎无法更改已停止容器的进程,这至少是由于其内部结构所致: https//github.com/docker/docker/issues / 1437 )。 So, basically the only option is to commit the container to an image and run it with a different command. 因此,基本上唯一的选择是将容器commit到映像并使用其他命令run它。

See https://forums.docker.com/t/run-command-in-stopped-container/343 参见https://forums.docker.com/t/run-command-in-stopped-container/343
(I believe the " ENTRYPOINT with arguments" approach wouldn't work either, since you still wouldn't be able to change the arguments to a stopped container.) (我相信“带有参数的ENTRYPOINT ”方法也不起作用,因为您仍然无法将参数更改为已停止的容器。)


#6楼

To expand on katrmr's answer, if the container is stopped and can't be started due to an error, you'll need to commit it to an image. 要扩展katrmr的答案,如果容器由于错误而停止并且无法启动,则需要将其commit到映像。 Then you can launch bash in the new image: 然后,您可以在新映像中启动bash:

docker commit [CONTAINER_ID] temporary_image
docker run --entrypoint=bash -it temporary_image
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值