Home > Enterprise >  Docker how to get shell on a container that exits fast
Docker how to get shell on a container that exits fast

Time:06-09

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?

CodePudding user response:

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.

  • Related