无法附加到运行 Docker 容器的 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43625255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:04:35  来源:igfitidea点击:

Can't attach to bash running the Docker container

linuxbashshelldockercontainers

提问by Ruslan Sakevych

Having troubles with attaching to the bash instance keeping the container running.

在附加到 bash 实例以保持容器运行时遇到问题。

To be more detailed. I am running container as here:

要更详细。我在这里运行容器:

$ docker run -dt --name test ubuntu bash

Now it should be actually running, not finished.

现在它应该实际运行,而不是完成。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             
STATUS              PORTS               NAMES
f3596c613cfe        ubuntu              "bash"              4 seconds ago       Up 2 seconds                            test

After this, I am trying to attach to that instance of bash that keeps the container running. Like this:

在此之后,我试图附加到保持容器运行的 bash 实例。像这样:

$ docker attach test

Running this command I am able to write something to stdin, but no result following. I am not sure if bash is getting lines I typed.

运行此命令,我可以向 stdin 写入一些内容,但没有结果。我不确定 bash 是否收到我输入的行。

Is there some other way to bash that keeps the container running?

有没有其他方法可以让容器保持运行?

I know, that I can run a different instance of bash and use it docker exec -it test bash. But being more general, is there a way to connect to process that's running in Docker container?

我知道,我可以运行不同的 bash 实例并使用它docker exec -it test bash。但更笼统地说,有没有办法连接到在 Docker 容器中运行的进程?

Sometimes it can be useful to save the session of a process running inside the container.

有时保存在容器内运行的进程的会话会很有用。

SOLUTION

解决方案

Thanks to user2915097for pointing out the missing -iflag.

感谢您user2915097指出丢失的-i标志。

So now we can have persistent bash session. For example, let's set some aliasand reuse after stopping and restarting the container.

所以现在我们可以有持久的 bash 会话。例如,让我们设置一些alias并在停止和重新启动容器后重用。

$ docker run -itd --name test ubuntu bash

To attach to bashinstance just run

要附加到bash实例,只需运行

$ docker attach test
root@3534cbe1e994:/# alias test="Hello, world!"

To detach from container and not to stop the container press Ctrl+p, Ctrl+q

要从容器中分离而不是停止容器,请按Ctrl+ p, Ctrl+q

Then we can stop and restart the container

然后我们可以停止并重新启动容器

$ docker stop test
$ docker start test

Now we can attach to the same bashinstance and check our alias

现在我们可以附加到同一个bash实例并检查我们的别名

$ docker attach test
root@3534cbe1e994:/# test
Hello, world!

Everything is working perfectly!

一切正常!

As I have pointed out in my comment use-case for this can be running some interactive shells as bash, octave, ipythonin Docker container persisting all the history, imports, variables and temporary settings just by reattaching to the same instance.

正如我在我的评论用例此所指出的,可以运行一些互动壳为bashoctaveipython在泊坞容器仅仅通过重新连接到同一个实例坚持所有的历史,进口,变量和临时设置。

采纳答案by user2915097

Your container is running, it is not finished, as you can see

您的容器正在运行,但尚未完成,如您所见

  • it appears in docker ps, so it is a running container
  • it show up n seconds
  • 它出现在 中docker ps,所以它是一个正在运行的容器
  • 它出现 n 秒

you launch it with -dtso you want it

你启动它-dt所以你想要它

detached (for d) allocate a tty (for t)

分离(对于 d)分配一个 tty(对于 t)

but not interactive, as you do not add -i

但不是交互式的,因为你没有添加 -i

Usually, you nearly always provide -ittogether, it may be -idt

通常,您几乎总是-it一起提供,可能是-idt

See this thread

看到这个线程

When would I use `--interactive` without `--tty` in a Docker container?

我什么时候会在 Docker 容器中使用没有 `--tty` 的 `--interactive`?

as you want bash, I think you should add -i

当你想要 bash 时,我认为你应该添加 -i

I am not sure why you use -d

我不确定你为什么使用 -d

Usually it is

通常是

docker run -it --rm --name=mytest ubuntu bash

docker run -it --rm --name=mytest ubuntu bash

and you can test

你可以测试

回答by King Chung Huang

A container's running lifecycle is determined by its root process, which is bash in your example. When your start your ubuntu container with bash as the process, bash is immediately exiting because it has nothing to keep it running. That's why the container immediately exits and there's nothing to attach to.

容器的运行生命周期由其根进程决定,在您的示例中为 bash。当您使用 bash 作为进程启动 ubuntu 容器时,bash 会立即退出,因为它没有任何东西可以让它继续运行。这就是为什么容器会立即退出并且没有任何东西可以附加的原因。