# How to view the contents of docker images?

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`.

[ad-logs-small]

## View contents while the container is running

1. Open your terminal and run the following command to start a container from the Docker image:
    
    ```bash
    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:
    
    ```bash
    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:
    
    ```bash
    docker exec <container_name> cat /path/to/file
    ```
    
3. Once you are done, you can stop and remove the container using the following command:
    
    ```bash
    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:

```bash
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:

```bash
docker export my-container | tar t > my-container-files.txt
```

Third option is to view the docker image history:

```bash
docker image history my-image:latest
```

[ad-uptime]