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
sha256:dce5666f3491f97fa62e17ec124390ecd93de2fa771b15653fc61e565255e67d

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:

CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR

Details are in Dockers docs: https://docs.docker.com/engine/reference/commandline/commit/