Enter an exited docker container in interactive mode

I have an exited docker container Exited (1) 8 seconds ago. And I would like to enter it in interactive mode (in bash mode) to check what went wrong how do I do that?

I know I can start an image (docker run -it IMAGE_NAME /bin/bash) to enter the interactive mode directly.

How do I re-run an exited container and enter the interactive mode? docker start -a CONTAINERID will cause it to exit 1 again. Because there is a automated command to start by default which specified in Dockerfile as CMD java ...


Solution 1:

You can turn the current state of a container into an image using docker commit command.

For example, if that were the way you ran the original image

$ docker run ubuntu touch what_went_wrong_file

you could use docker ps to see the name of the container

$ docker ps -a -n 1 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
abcdef012345        ubuntu              "touch what_went_wro…"   42 seconds ago      Exited (0) 23 seconds ago                       ron_pringadi

and then commit its changes

$ docker commit abcdef012345 what_went_wrong_image # or
$ docker commit ron_pringadi what_went_wrong_image

Now that you have an image you can run it in a new container and explore its content

$ docker run -it what_went_wrong_image bash
root@01234abcdef:/# ls -l what_went_wrong_file
-rw-r--r-- 1 root root 0 Feb 31 24:00 what_went_wrong_file