When working with Docker images, you might wonder:
“What exactly is inside this image?”
Whether you’re auditing dependencies, debugging issues, or just curious, it’s useful to inspect the contents of a Docker image—such as files, directories, binaries, and installed packages.
In this post, we’ll explore several ways to view the contents of a Docker image using Docker CLI, containers, and third-party tools.
✅ Method 1: Use docker run
or docker create
+ docker exec
The easiest way to inspect an image is to start a container from it and browse its file system.
Step-by-step:
docker run -it --rm your-image-name sh
Or use bash
if the image supports it:
docker run -it --rm your-image-name bash
Then inside the container:
ls /
ls /app
cat /etc/os-release
💡
--rm
ensures the container is removed after exit
💡 Usesh
for Alpine or minimal images,bash
for Debian/Ubuntu-based ones
✅ Method 2: Use docker export
+ tar
If you prefer to inspect the filesystem outside of a running container:
# Create a container (but don’t start it)
container_id=$(docker create your-image-name)
# Export its filesystem
docker export $container_id -o image.tar
# Extract contents
mkdir image-contents
tar -xf image.tar -C image-contents
# Browse the extracted folder
ls image-contents/
✅ This is great for scripting, security scans, or offline inspection
✅ Method 3: Use Dive — A Visual Layer Explorer
dive
is a CLI tool that lets you analyze Docker image layers, view file diffs, and see how each command in the Dockerfile affects the image.
Install Dive:
brew install dive # macOS
sudo apt install dive # Ubuntu (if available)
Then run:
dive your-image-name
You’ll see:
- Layer-by-layer file system changes
- File sizes and where space is used
- Which files were added/modified at each layer
🔍 Perfect for optimizing images and reducing size!
✅ Method 4: Use docker history
(Metadata Only)
For a high-level view of how the image was built:
docker history your-image-name
Output:
IMAGE CREATED CREATED BY SIZE COMMENT
<id> 2 hours ago /bin/sh -c apt-get… 34MB install packages
You’ll see all the RUN
, COPY
, and ADD
instructions and how much each contributed to the image size.
❗ This doesn’t show actual files, just build commands and sizes.
📝 Conclusion
Docker images may be black boxes at first—but with the right tools, you can explore, analyze, and understand what’s inside. Whether you’re debugging, optimizing, or auditing, knowing how to inspect image contents gives you greater control and confidence in your containerized apps.
🔑 Summary
Task | Method |
---|---|
Quick inspection | docker run -it image-name sh |
Extract full filesystem | docker export + tar |
Layer-by-layer analysis | dive |
View image build history | docker history image-name |