How to list files in a stopped Docker container

How to list files in a stopped Docker Container

Today in this article, we will see how to list files in a stopped Docker Container.

You might find the need to view, refer or explore files from running containers or stopped containers on multiple occasions.

In this article, we will see a couple of approaches that could be useful to view the files in paused or stopped containers,

  • Commit Stopped Container to a new image
  • Copy files from Container to Host

Approach 1 – Commit Stopped Container to a new image

Committing a container’s file changes or settings into a new image could be useful notable times.

This feature lets you debug a container or help you to export a working dataset to other servers.

Please see the below steps to use the commit command.

Step1 Commit the stopped container to a new image,

Command

docker commit <CONTAINER_ID> <New Image Name>

Example

docker commit 1cf2eafdee49 backup-container

Above ‘backup-container’ is the new image

Step2 Run the new image in a new container

Run the new image in a new container with a bash-shell or shell

Command

docker run -ti --entrypoint=bash <new image>

Step 3Run the list file command in the new container.

explored docker files

Approach 2 – Copy Files from Docker Container to Host

Docker “cp” utility let you copy files from the container’s file system to the local machine or from the local filesystem to the container.

docker cp <container>:<container-path> <host-path>

cp command to work, Containers can be a running or stopped

Below we are copying the file from the container to the host path.

Once copied, the host path can be used to explore the files.

list files in paused Docker Container

Below is now how you can check all the files and directory

dir path

How to list files in a stopped Docker Container

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *