0
0
Dockerdevops~5 mins

Analyzing image layers with dive in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build Docker images, they are made of layers. Sometimes these layers get too big or inefficient. Dive helps you look inside these layers to see what files are added or changed, so you can make your images smaller and faster.
When you want to find out why your Docker image is very large.
When you want to see which files are added in each layer of your Docker image.
When you want to optimize your Dockerfile to reduce image size.
When you want to check if unnecessary files are included in your image.
When you want to understand how your Docker image is built step-by-step.
Commands
This command builds a Docker image named 'example-app' using the Dockerfile in the current folder. We do this first to have an image to analyze with dive.
Terminal
docker build -t example-app:latest .
Expected OutputExpected
Sending build context to Docker daemon 3.07kB Step 1/3 : FROM alpine:3.18 3.18: Pulling from library/alpine Digest: sha256:... Status: Downloaded newer image for alpine:3.18 ---> 3f53bb00af94 Step 2/3 : COPY app.sh /app.sh ---> Using cache ---> 8c7f9a1f9e2a Step 3/3 : CMD ["/app.sh"] ---> Running in 4d5a6b7c8d9e Removing intermediate container 4d5a6b7c8d9e ---> 1a2b3c4d5e6f Successfully built 1a2b3c4d5e6f Successfully tagged example-app:latest
-t - Names and tags the image for easy reference.
This command opens dive to analyze the 'example-app:latest' image. Dive shows each layer, what files it adds or changes, and the size impact.
Terminal
dive example-app:latest
Expected OutputExpected
Launching dive UI... [UI opens showing image layers, file changes, and size details]
Key Concept

If you remember nothing else from this pattern, remember: dive lets you see inside Docker image layers to find and fix size and efficiency problems.

Common Mistakes
Trying to run dive without building the Docker image first.
Dive needs an existing image to analyze; without it, dive will show an error or no data.
Always build your Docker image with 'docker build' before running dive.
Not tagging the image during build and then using the wrong image name in dive.
Dive cannot find the image if the name or tag is incorrect.
Use the exact image name and tag you set with 'docker build -t' when running dive.
Summary
Build your Docker image with 'docker build -t example-app:latest .' to create an image to analyze.
Run 'dive example-app:latest' to open a visual tool showing each image layer and its contents.
Use dive to find large or unnecessary files in layers and improve your Dockerfile for smaller images.