In this lesson, you will learn how to look "under the hood" of your container images to see exactly which Dockerfile instructions created specific filesystem layers and how much space they consume.
Understanding the Blueprint
Every time you build an image using a Dockerfile, Docker creates immutable layers for every instruction (like RUN, COPY, or ADD). Because these layers are stacked on top of each other using a Union File System, it is important to know which parts of your build are adding "weight" to your final image.
Using the docker history Command
The most direct way to inspect the metadata of your image layers is the docker history command. This command displays the history of an image, showing each layer’s size, creation time, and the command that generated it.
Try running this on a standard image like alpine:
docker history alpine
When you run this, you will see output structured like this:
- IMAGE: The unique ID of the layer.
- CREATED: How long ago that layer was created.
- CREATED BY: The specific Dockerfile instruction (e.g.,
/bin/sh -c #(nop) ADD file:...). - SIZE: The size of that specific layer.
Identifying Bloat
If you see a layer with a large size, you know immediately that a specific RUN command in your Dockerfile (such as downloading a massive library or moving large files) contributed significantly to your image size. By identifying these heavy layers, you can optimize your build process—for example, by combining multiple RUN commands into one to ensure that temporary files created and deleted in the same layer do not take up space in the final image.
The docker history command provides the transparency needed to debug "bloated" images. If you need even more granular detail about the image structure, such as environment variables or exposed ports, you can use docker inspect <image_name>. However, for mapping commands to disk usage, history is your primary diagnostic tool.
Key Takeaway: Use docker history to map your Dockerfile instructions to specific filesystem layers, allowing you to identify which commands are responsible for image size and layer bloat.