简体   繁体   中英

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.

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. However the above approach is pretty straight forward and can be replicated using compose file as well.

NOTE: if you still want to run the <someCommandWhichExists> just run it on bash terminal that gets open up.

Second Way :

Let us say xcontainerid container already exited from output of 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

This ximagename will have the exact environment as of your exited xcontainerid .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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