Sub topic 6
Measuring size with docker image ls and docker history
This chapter assumes you can build an image with docker build, run containers with docker run, and read a simple Dockerfile. You should also know that an image is a stack of layers. Before you shrink an image, you need to measure it. Two commands cover most of the work: docker image ls shows the size of whole images, and docker history shows the size of each layer.
Comparing images with docker image ls
docker image ls lists local images and their sizes. Pass a repository name to filter the list.
docker build -t myapp:small .
docker build --target build -t myapp:build .
docker image ls myappThe second build uses --target build to stop at the stage named build. It gives you a second image that contains the build tools. Listing both images side by side shows what the final stage left out. The output looks like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp small 3f1c2a9b7d10 10 seconds ago 12.4MB
myapp build a82e55c0d4be 40 seconds ago 1.1GBThe numbers above are examples. Your sizes will differ.
The SIZE column is the disk space the image uses. Layers shared with other images are stored once on disk, so adding up the sizes of several images overstates the real disk use. Use the column to compare one image against another, not to compute a total.
Inspecting layers with docker history
docker history lists the layers of one image, newest first. Each row shows the instruction that created the layer and the size that layer added.
docker history myapp:smallIMAGE CREATED CREATED BY SIZE
3f1c2a9b7d10 2 minutes ago ENTRYPOINT ["/app"] 0B
<missing> 2 minutes ago COPY /out/app /app # buildkit 8.2MBRead the SIZE column from the top and look for rows that stand out. Instructions that only set metadata, such as ENTRYPOINT, ENV and WORKDIR, add 0B. Instructions that write files, such as COPY and RUN, add the size of what they wrote.
The CREATED BY text is cut off by default. Add --no-trunc to see the full command, which helps when several RUN lines look alike.
docker history --no-trunc myapp:buildThe <missing> value in the IMAGE column is normal. Layers built on another machine, or by BuildKit, have no local image ID. It does not mean the image is broken.
A workflow for finding what is big
- Build the image and run
docker image lsto get a baseline size. - Run
docker historyand find the layers with the largestSIZE. - Match each large layer to its instruction in the Dockerfile.
- Change that instruction, rebuild, and compare the new size with the baseline.
Change one thing at a time. If you change several instructions at once, you cannot tell which change helped.
Common errors
- Deleting files in a later layer. A
RUN rm -rf /tmp/cachein a new layer does not shrink the image. The files still exist in the earlier layer. Remove them in the sameRUNinstruction that created them, or keep them out of the final stage. - Reading the wrong tag. After a rebuild, an old tag can still point at the previous image. Check the
CREATEDcolumn to confirm you are looking at the new build. - Adding up image sizes. Shared layers are counted in each image's size, so the sum is not the disk usage.
Exercise
You add RUN rm -rf /var/lib/apt/lists/* as its own line after an apt-get install line. The image size does not change. Using docker history, what would you see, and how do you fix it?
Show answer
docker history would show the apt-get install layer still holding the package lists, and the rm layer adding 0B or close to it. Deleting a file in a later layer only hides it. Run the install and the cleanup in the same RUN instruction, joined with &&, so the files never get stored in a layer.