88

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.

Nathan
  • 8,093
  • 8
  • 50
  • 76

7 Answers7

112

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
Nathan
  • 8,093
  • 8
  • 50
  • 76
  • 6
    This is neat if you can start the docker, but if it's blocked and you can't start it, that's really not helpful. Is there really no way to list the files of a docker that isn't running?! – Alexis Wilke Apr 09 '20 at 19:35
  • 2
    @AlexisWilke I have never encountered a blocked container. Since this solution works for all of my use cases, I have never tried to figure out how to list the files or copy a file out of a container. If you figure out a solution, please post an answer. You could try posting a question with your specific situation. – Nathan Apr 10 '20 at 14:27
  • 1
    @AlexisWilke is right about the container to be running at the time I need to list the files. Anyway it worked for me to run `docker commit $CONTAINER_ID test_image` as it created the test_image container and I went inside the container to list the files. It helped to use Docker Desktop because I didn't need the 2nd and 3rd steps. – Roberto C. Rodriguez-Hidalgo May 30 '21 at 21:31
  • 1
    you saved my life man!! I lost the original files of my project and all I had is the docker containers, which I couldn't run without having the docker files. thanx to your solution I was able to retrieve the data from inside the exited containers. – LazerDance Jun 07 '21 at 16:13
25

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

LVillaca
  • 351
  • 3
  • 6
  • 2
    The command `docker diff` will list the added, deleted and changed files and directories since a Container started. This is much faster than exporting. – Nathan Aug 31 '20 at 14:36
19

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.

Nathan
  • 8,093
  • 8
  • 50
  • 76
12

If you want to see a certain file content, I would suggest using docker container cp command. Here is the doc. It works on stopped container. Example:

docker container cp 02b1ef7de80a:/etc/nginx/conf.d/default.conf ./

This way I got the config file that was generated by templating engine during start.

Azat Khadiev
  • 387
  • 4
  • 14
  • 2
    This answer is already mentioned in the link in the question. It does not answer the question. The question is to list the files in a directory in a stopped container. The question is not about how to copy the file. – Nathan Apr 28 '20 at 14:35
  • docker container cp : - Notice the "-" at the end of the command. It actually "copies" the specified file from the stopped container into "stdout". In other words, it just prints the file contents. Thanks @azat-khadiev for your direction (I don't know why you got "-1 for that answer...) – David Peleg Aug 31 '20 at 09:02
  • @DavidPeleg This answer and your comment do not answer the question. The question is about *listing* the files and not about *copying* the files. – Nathan Aug 31 '20 at 14:10
  • Please post your answer to this question. https://stackoverflow.com/questions/34429757/how-to-copy-file-to-stopped-docker-container – Nathan Aug 31 '20 at 14:11
3
docker container cp <STOPPED_CONTAINER_ID>:<PATH_TO_FILE> -

Notice the "-" at the end of the command.

It actually "copies" the specified file from the stopped container into "stdout". In other words, it just prints the file contents.

Thanks @azat-khadiev for your direction (I don't know why you got "-1 for that answer...)

David Peleg
  • 394
  • 2
  • 9
  • This answer does not answer the question. The question is to list the files in a directory in a stopped container. The question is not about how to copy the file. The link in the question is where this answer belongs. – Nathan Aug 31 '20 at 14:09
  • Please post your answer to this question. https://stackoverflow.com/questions/34429757/how-to-copy-file-to-stopped-docker-container – Nathan Aug 31 '20 at 14:12
2

Try using container-diff with the --type=file option. This will compare two images and report the files added, deleted and modified.

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.

This tool requires that you first create an image of the stopped Docker container with docker commit.

Here is the command to install it:

curl -LO https://storage.googleapis.com/container-diff/latest/container-diff-linux-amd64 \
    && chmod +x container-diff-linux-amd64 \
    && mkdir -p $HOME/bin \
    && export PATH=$PATH:$HOME/bin \
    && mv container-diff-linux-amd64 $HOME/bin/container-diff

Here is the command to use the utility:

container-diff analyze $IMAGE --type=file
Nathan
  • 8,093
  • 8
  • 50
  • 76
Exp3ct_m3
  • 31
  • 2
  • The command `docker diff` will list the added, deleted and changed files and directories since a Container started. This is much faster than creating an image, installing a tool and running the tool. I added this as another answer. – Nathan Aug 31 '20 at 14:31
0

In case you are working with docker desktop, you can view all by clicking on the the container in question and the going to files. This is extremely fast and easy, I am happy I found this possibility... all files visible using docker desktop It also works for exited containers.

Noskario
  • 378
  • 1
  • 9