✦ For everyone, free.

Practical knowledge for real and everyday life

Home

3.1.2.5 Image Layer Size

A focused guide to Image Layer Size, connecting core concepts with practical Docker and container operations.

Image layer size is the amount of disk space a specific layer adds to an image, determined by exactly what filesystem changes that layer's instruction introduced, and it is one of the main factors determining an image's total size and how quickly it can be pulled or pushed.

Measuring Per-Layer Size

Each layer's individual contribution to an image's total size can be inspected directly, which is useful for identifying which specific instructions are responsible for most of an image's footprint.

docker history myapp:1.0

This lists every layer along with the size it added, making it straightforward to spot a single instruction that disproportionately increased the image's overall size.

Common Causes of Unexpectedly Large Layers

Installing build tools that are never removed, copying files unnecessarily included in the build context, or failing to clean up package manager caches within the same instruction that installed them are common reasons a layer ends up larger than it needs to be.

RUN apt-get update && apt-get install -y build-essential && pip install -r requirements.txt
FROM python:3.12 AS build
RUN apt-get update && apt-get install -y build-essential
RUN pip wheel -r requirements.txt -w /wheels

FROM python:3.12-slim
COPY --from=build /wheels /wheels
RUN pip install --no-index --find-links=/wheels -r requirements.txt

The multi-stage version avoids ever including build-essential in the final image's layers at all, rather than installing and later attempting to remove it within the same image.

Cleaning Up Within the Same Layer

Because a deleted file in a later layer does not reduce an image's size if it was added in an earlier layer, cleanup steps need to happen within the same RUN instruction that created the data being cleaned up, not in a separate, later instruction.

RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

Combining installation and cleanup into a single instruction ensures the resulting single layer reflects only the net, post-cleanup state.

Why Layer Size Matters

Smaller layers mean faster image pulls, faster pushes, and less disk space consumed across every host and registry storing the image — at scale, the cumulative effect of unnecessarily large layers can meaningfully slow down deployments and increase storage costs.