In this lesson, you will learn how to use the open-source tool dive to peek inside your Docker image layers and identify exactly what is bloating your container size.
Why standard tools aren't enough
You already know that Docker images are built in layers, and that commands like RUN, COPY, and ADD each create a new filesystem layer. While docker history shows you the commands used to build an image, it doesn't give you a clear look at what changed inside the files. If you have a large image and don't know why, dive is your best friend.
Exploring layers with Dive
dive provides an interactive TUI (Terminal User Interface) that lets you navigate through each layer of your image. When you run dive <image-name>, you get three panels:
- Layer List (Left): Shows every step of your build process.
- File Tree (Right): Displays the files added, modified, or deleted in the selected layer.
- Layer Details (Bottom): Shows size statistics and identifies "inefficiencies" (like files added and then deleted in the same layer).
Imagine you accidentally copied a large local folder into your image only to delete it in the next RUN command. Even though it's "gone," the data still exists in the previous layer, taking up space. dive will highlight these modified files in yellow or red, making it obvious where your storage is being wasted.
Practical Workflow
After installing dive, run it against one of your images:
dive my-app:latest
Use the Up/Down arrows to move through the layers. As you click on each layer, watch the File Tree to see if you are accidentally including unnecessary logs, build caches, or source code directories. By spotting these files, you can go back to your Dockerfile and update your .dockerignore file or chain your RUN commands to delete temporary files before the layer is finalized.
Key Takeaway: Use dive to visually inspect your image layer-by-layer, allowing you to pinpoint hidden files and inefficient build steps that are unnecessarily increasing your container's footprint.