How to list files in a stopped Docker container

This question shows how to copy files out of a stopped container. This requires that I know the full path to the file including its file name. I know the directory I want to copy a file out of, but I do not know its file name since that is generated dynamically. How do I list the files in a directory in a stopped Docker container?

The following Docker command works great if the Docker container is running. But, it fails if the Docker container is stopped.

docker exec --privileged MyContainer ls -1 /var/log

Note: The files are not stored in a persistent volume.


Solution 1:

This answer to another question shows how to start a stopped container with another command. Here are the commands to list files in a stopped container.

  1. Commit the stopped container to a new image: test_image.
    • docker commit $CONTAINER_ID test_image
  2. Run the new image in a new container with a shell.
    • docker run -ti --entrypoint=sh test_image
  3. Run the list file command in the new container.
    • docker exec --privileged $NEW_CONTAINER_ID ls -1 /var/log

Solution 2:

When starting the container is not an option, you can always export your image (a bit overkill but..) and list its contents:

docker export -o dump.tar <container id>

tar -tvf dump.tar

Reference: Baeldung - Exploring a Docker Container’s Filesystem

Solution 3:

The command docker diff *CONTAINER* will list the files added, deleted and changed since the Container started.

If a file did not change since the container was started, then you would have to know the contents of the original image that started the container. So, this answer is not ideal but avoids creating an image and running it.

Unlike container-diff, this command does not require first creating a Docker image.