How to view the contents of docker images?

Better Stack Team
Updated on April 14, 2023

To view the contents of a Docker image, you can use the docker run command to start a container from the image and then use commands such as ls or cat to view the contents of the container. If you prefer not to start the container, there is an option for docker image save command.

View contents while the container is running

  1. Open your terminal and run the following command to start a container from the Docker image:

     
    docker run --name <container_name> <image_name>
    

    Replace <container_name> with a name for your container, and <image_name> with the name of the Docker image you want to view the contents of.

  2. Once the container is running, you can use commands such as ls or cat to view the contents of the container. For example, to list the contents of the root directory, you can run:

     
    docker exec <container_name> ls /
    

    This command will show you the files and directories in the root directory of the container.

    You can also view the contents of a specific file by running the cat command followed by the path to the file. For example:

     
    docker exec <container_name> cat /path/to/file
    
  3. Once you are done, you can stop and remove the container using the following command:

     
    docker stop <container_name> && docker rm <container_name>
    

    This will stop and remove the container from your system.

View contents without starting the container

Use the following command to directly save the contents of the image to a tar file:

 
docker image save my-image:latest > my-image.tar

You can also export the container's filesystem. If you don’t need to save or open the archive, instead preferring to get the file list in your terminal, modify the tar command:

 
docker export my-container | tar t > my-container-files.txt

Third option is to view the docker image history:

 
docker image history my-image:latest
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack