如何在现有的Docker容器上运行命令?

时间:2022-05-08 20:45:23

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. 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。我不能赢。

11 个解决方案

#1


335  

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. If the container is currently stopped, you need to first run it with the following command:

注意,exec命令只在已经运行的容器上工作。如果容器当前被停止,您需要首先使用以下命令运行它:

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

The most important thing here is the -d option, which stands for 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.

这里最重要的是-d选项,它代表分离。这意味着您最初提供给容器的命令(/bin/bash)将在后台运行,容器不会立即停止。

#2


235  

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.
  • -即使没有附上,我也要打开。
  • -t Allocate a pseudo-TTY.
  • - t分配pseudo-TTY。

So your new run command is:

所以你的新运行命令是:

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作为命令。

#3


74  

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. Then you can launch bash in the new image:

要扩展katrmr的答案,如果容器被停止并且由于错误不能启动,您需要将其提交给一个映像。然后你可以在新图像中启动bash:

docker commit [CONTAINER_ID] temporary_image
docker run --entrypoint=bash -it temporary_image

#4


35  

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). So, basically the only option is to commit the container to an image and run it with a different command.

Sven Dowideit在Docker论坛上解释说,容器是绑定到他们的进程的(并且Docker不能改变一个已停止的容器的进程,似乎至少是由于它的内部结构:https://github.com/docker/docker/es/1437)。所以,基本上唯一的选择是将容器提交给一个映像并使用另一个命令运行它。

See 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.)

请参见https://forums.docker.com/t/run-command-in-stopped-container/343(我相信“带有参数的ENTRYPOINT”方法也不会起作用,因为您仍然无法将参数更改为已停止的容器)。

#5


34  

So I think the answer is simple than many misleading answers above.

所以我认为答案很简单,比上面许多误导的答案都简单。

To start an existing container which is stopped

启动已停止的现有容器。

docker start <container-name/ID>

To stop a running container

停止运行的容器。

docker stop <container-name/ID>

Then to login to the interactive shell of a container

然后登录到一个容器的交互式shell中。

docker exec -it <container-name/ID> bash

#6


12  

I had to use bash -c to run my command: docker exec -it CONTAINER_ID bash -c "mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql"

我必须使用bash -c来运行我的命令:docker exec - CONTAINER_ID bash -c“mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql”

#7


9  

Creating a container and sending commands to it, one by one:

创建一个容器并向它发送命令,一个接一个:

docker create --name=my_new_container -it ubuntu
docker start my_new_container
// ps -a says 'Up X seconds'
docker exec my_new_container /path/to/my/command
// ps -a still says 'Up X+Y seconds'
docker exec my_new_container /path/to/another/command

#8


6  

This is a combined answer I made up using the CDR LDN answer above and the answer I found here.

这是我用CDR LDN的答案和我在这里找到的答案组合起来的答案。

The following example starts an Arch Linux container from an image, and then installs git on that container using the pacman tool:

下面的示例从映像启动一个Arch Linux容器,然后使用pacman工具在该容器上安装git:

sudo docker run -it -d archlinux /bin/bash
sudo docker ps -l
sudo docker exec -it [container_ID] script /dev/null -c "pacman -S git --noconfirm"

That is all.

这是所有。

#9


2  

Assuming the image is using the default entrypoint /bin/sh -c, running /bin/bash will exit immediately in daemon mode (-d). If you want this container to run an interactive shell, use -it instead of -d. If you want to execute arbitrary commands in a container usually executing another process, you might want to try nsenter or nsinit. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for the details.

假设图像使用默认的entrypoint /bin/sh -c,运行/bin/bash将立即在守护模式(-d)中退出。如果您希望这个容器运行一个交互式shell,可以使用它,而不是-d。如果您想在容器中执行任意命令,通常执行另一个进程,您可能想尝试nsenter或nsinit。查看https://blog.codecentric.de/en/2014/07/enter-docker-container/获取详细信息。

#10


2  

Unfortunately it is impossible to override ENTRYPOINT with arguments with docker run --entrypoint to achieve this goal.

不幸的是,要用docker运行的参数来重写ENTRYPOINT是不可能的——入口点来实现这个目标。

Note: you can override the ENTRYPOINT setting using --entrypoint, but this can only set the binary to exec (no sh -c will be used).

注意:您可以使用- ENTRYPOINT覆盖ENTRYPOINT设置,但是这只能将二进制文件设置为exec(不使用sh -c)。

#11


0  

Simple answer: start and attach at the same time. In this case you are doing exactly what you asked for.

简单的回答:同时开始和附加。在这种情况下,你所做的正是你所要求的。

docker start <CONTAINER_ID/CONTAINER_NAME> && docker attach <CONTAINER_ID/CONTAINER_NAME> 

make sure to change <CONTAINER_ID/CONTAINER_NAME>

确保更改

#1


335  

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. If the container is currently stopped, you need to first run it with the following command:

注意,exec命令只在已经运行的容器上工作。如果容器当前被停止,您需要首先使用以下命令运行它:

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

The most important thing here is the -d option, which stands for 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.

这里最重要的是-d选项,它代表分离。这意味着您最初提供给容器的命令(/bin/bash)将在后台运行,容器不会立即停止。

#2


235  

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.
  • -即使没有附上,我也要打开。
  • -t Allocate a pseudo-TTY.
  • - t分配pseudo-TTY。

So your new run command is:

所以你的新运行命令是:

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作为命令。

#3


74  

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. Then you can launch bash in the new image:

要扩展katrmr的答案,如果容器被停止并且由于错误不能启动,您需要将其提交给一个映像。然后你可以在新图像中启动bash:

docker commit [CONTAINER_ID] temporary_image
docker run --entrypoint=bash -it temporary_image

#4


35  

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). So, basically the only option is to commit the container to an image and run it with a different command.

Sven Dowideit在Docker论坛上解释说,容器是绑定到他们的进程的(并且Docker不能改变一个已停止的容器的进程,似乎至少是由于它的内部结构:https://github.com/docker/docker/es/1437)。所以,基本上唯一的选择是将容器提交给一个映像并使用另一个命令运行它。

See 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.)

请参见https://forums.docker.com/t/run-command-in-stopped-container/343(我相信“带有参数的ENTRYPOINT”方法也不会起作用,因为您仍然无法将参数更改为已停止的容器)。

#5


34  

So I think the answer is simple than many misleading answers above.

所以我认为答案很简单,比上面许多误导的答案都简单。

To start an existing container which is stopped

启动已停止的现有容器。

docker start <container-name/ID>

To stop a running container

停止运行的容器。

docker stop <container-name/ID>

Then to login to the interactive shell of a container

然后登录到一个容器的交互式shell中。

docker exec -it <container-name/ID> bash

#6


12  

I had to use bash -c to run my command: docker exec -it CONTAINER_ID bash -c "mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql"

我必须使用bash -c来运行我的命令:docker exec - CONTAINER_ID bash -c“mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql”

#7


9  

Creating a container and sending commands to it, one by one:

创建一个容器并向它发送命令,一个接一个:

docker create --name=my_new_container -it ubuntu
docker start my_new_container
// ps -a says 'Up X seconds'
docker exec my_new_container /path/to/my/command
// ps -a still says 'Up X+Y seconds'
docker exec my_new_container /path/to/another/command

#8


6  

This is a combined answer I made up using the CDR LDN answer above and the answer I found here.

这是我用CDR LDN的答案和我在这里找到的答案组合起来的答案。

The following example starts an Arch Linux container from an image, and then installs git on that container using the pacman tool:

下面的示例从映像启动一个Arch Linux容器,然后使用pacman工具在该容器上安装git:

sudo docker run -it -d archlinux /bin/bash
sudo docker ps -l
sudo docker exec -it [container_ID] script /dev/null -c "pacman -S git --noconfirm"

That is all.

这是所有。

#9


2  

Assuming the image is using the default entrypoint /bin/sh -c, running /bin/bash will exit immediately in daemon mode (-d). If you want this container to run an interactive shell, use -it instead of -d. If you want to execute arbitrary commands in a container usually executing another process, you might want to try nsenter or nsinit. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for the details.

假设图像使用默认的entrypoint /bin/sh -c,运行/bin/bash将立即在守护模式(-d)中退出。如果您希望这个容器运行一个交互式shell,可以使用它,而不是-d。如果您想在容器中执行任意命令,通常执行另一个进程,您可能想尝试nsenter或nsinit。查看https://blog.codecentric.de/en/2014/07/enter-docker-container/获取详细信息。

#10


2  

Unfortunately it is impossible to override ENTRYPOINT with arguments with docker run --entrypoint to achieve this goal.

不幸的是,要用docker运行的参数来重写ENTRYPOINT是不可能的——入口点来实现这个目标。

Note: you can override the ENTRYPOINT setting using --entrypoint, but this can only set the binary to exec (no sh -c will be used).

注意:您可以使用- ENTRYPOINT覆盖ENTRYPOINT设置,但是这只能将二进制文件设置为exec(不使用sh -c)。

#11


0  

Simple answer: start and attach at the same time. In this case you are doing exactly what you asked for.

简单的回答:同时开始和附加。在这种情况下,你所做的正是你所要求的。

docker start <CONTAINER_ID/CONTAINER_NAME> && docker attach <CONTAINER_ID/CONTAINER_NAME> 

make sure to change <CONTAINER_ID/CONTAINER_NAME>

确保更改