简体   繁体   English

附加到 docker 容器内的特定运行进程

[英]Attach to the specific running process inside a docker container

While the nginx might not be a good example for this case but there would be similar cases that we will need to run a process and access it inside a container without recreating/rerunning it.虽然 nginx 可能不是这种情况的好例子,但也有类似的情况,我们需要运行一个进程并在容器内访问它,而无需重新创建/重新运行它。

I already run nginx as a container using following command:我已经使用以下命令将 nginx 作为容器运行:

docker run -d --name=my_container nginx:latest

I didn't active terminal or interactive mode during docker run command.在 docker 运行命令期间,我没有激活终端或交互模式。 Now, I want to run a bash (using docker exec... ) in detach mode and then attach (using docker attach... ) to it later.现在,我想在分离模式下运行 bash(使用docker exec... ),然后稍后附加(使用docker attach... )。

As you know we can run new process inside a container, eg:如您所知,我们可以在容器内运行新进程,例如:

docker exec -itd my_container bash

By this way a new bash process will run inside this container.通过这种方式,一个新的 bash 进程将在这个容器内运行。

Now my question is that how to attach to this process later?现在我的问题是以后如何附加到这个过程?

I tried to run following command but it just shows the nginx live log:我尝试运行以下命令,但它只显示 nginx 实时日志:

docker attach my_nginx2

If I understand your use case, you can do this:如果我了解您的用例,您可以这样做:

docker run -itd --name=my_container nginx:latest bash -i -c 'nginx; bash -i'

this allows you to do:这使您可以:

docker attach my_container

you can detach from a container and leave it running using the CTRL-p CTRL-q key sequence.您可以使用 CTRL-p CTRL-q 键序列从容器中分离并使其保持运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM