How to view the contents of docker images?

Better Stack Team
Updated on November 23, 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

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github