简体   繁体   English

Docker如何在快速退出的容器上获取​​shell

[英]Docker how to get shell on a container that exits fast

I have an issue with a container that causes it to exit immediately.我有一个容器问题,导致它立即退出。 I usually run this command on the problematic container to get a shell inside them and investigate the issue.我通常在有问题的容器上运行此命令以在其中获取 shell 并调查问题。

docker exec -it <container_name> /bin/bash

But if the container fails almost immediately, this does not work.但是,如果容器几乎立即失败,这将不起作用。

How can I easily enter into the exited container so that I can find the problem?如何轻松进入退出的容器以便找到问题?

First Way :第一种方式

docker run -it <container_name> sleep 20200202

Then try然后尝试

docker exec -it <container_name> bash

If you have any entrypoint defined:如果您定义了任何入口点:

docker run -it --entrypoint "" <container_name> bash

You can also use other approaches of redirecting your command output to /dev/null ... so stdout never reads an exit.您还可以使用其他方法将您的命令输出重定向到/dev/null ...因此stdout永远不会读取退出。 However the above approach is pretty straight forward and can be replicated using compose file as well.但是,上述方法非常简单,也可以使用 compose 文件进行复制。

NOTE: if you still want to run the <someCommandWhichExists> just run it on bash terminal that gets open up.注意:如果您仍想运行<someCommandWhichExists> ,只需在打开的 bash 终端上运行它。

Second Way :第二种方式

Let us say xcontainerid container already exited from output of docker ps -a .假设xcontainerid容器已经从 docker docker ps -a的输出中退出。

If you are trying to get into this particular container, Just hit:如果你想进入这个特定的容器,只需点击:

docker commit xcontainerid ximagename docker run -it --entrypoint "" ximagename bash docker commit xcontainerid ximagename docker run -it --entrypoint "" ximagename bash

This ximagename will have the exact environment as of your exited xcontainerid .ximagename将具有与您退出的xcontainerid完全相同的环境。

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

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