How to look into a filesystem of a stopped Docker container

Sometimes, particularly when building new containers it would be good to have a look at the filesystem inside the container after it stopped or crashed, perhaps to have a look at the config files (if they got generated on the fly) or logs (if for some reason the processes there don't log to stdout). But if nothing is running inside the container exec is not going to work:

$ docker exec -it mycontainer2 /bin/bash
Error response from daemon: Container 7a524561fd0288418d6c748f8c3532be13f14fb32c94bcfca9ea9a83ebda5433 is not running

In order to have a look into the container we have to commit the container layer into a new image:

$ docker commit mycontainer2 myimage2

and then run it:

$ docker run --rm -it myimage2 /bin/bash
bash-4.4# ls

When the stopped container is turned into an image some changes can be made to it - the following Dockerfile commands can be modified:


Details are in Dockers docs: