How to Shrink Docker Images: Proven Techniques to Cut Size from 1.6 GB to 600 MB
This article explains the fundamentals of Docker image layers, demonstrates how to analyze layer size with tools like docker history and dive, and provides practical optimization techniques—including multi‑stage builds, cache removal, layer squashing, and metadata‑aware copying—to dramatically reduce image footprints.
Theoretical Basis
The essence of a Docker image is a compressed package of image layers and a runtime configuration file. Building an image creates new layers via RUN, COPY and ADD instructions. Each instruction generates a new layer that records all filesystem changes.
RUN , COPY and ADD create a new layer; all changes are committed as a whole.
Layers use copy‑on‑write: updating a file that exists in a lower layer copies it to the new layer, doubling storage for that file.
Deleting a file in a later layer only adds a deletion marker; the original file data remains in earlier layers and does not reduce image size.
FROM alpine:latest
COPY resource.tar /
RUN touch /resource.tar
RUN rm -f /resource.tar
ENTRYPOINT ["/bin/ash"]Building this Dockerfile and inspecting the image with docker history shows how each command contributes to layer size.
$ docker build -t test-image -f Dockerfile .
$ docker history test-image:latest
IMAGE CREATED CREATED BY SIZE COMMENT
95f1695b About a minute # (nop) ENTRYPOINT ["/bin/ash"] 0B
1780448c About a minute rm -f /resource.tar 0B
a85d29bf About a minute touch /resource.tar 135MB
6dac335f 4 minutes ago # (nop) COPY file:66065d6e23e0bc52… 135MB
...Analysis Tools
Two useful tools for inspecting image size:
docker history
The built‑in command lists each layer’s creation time, instruction and size, but can be limited for complex images.
dive
A third‑party tool that visualizes layer contents and highlights added, modified or deleted files.
Optimization Techniques
Multi‑stage Builds and Scratch
Separate build and runtime environments. Compile in a full SDK image, then copy the static binary into a minimal scratch (or alpine) image.
FROM golang
COPY hello.go .
ENV CGO_ENABLED=0
RUN go build hello.go
FROM scratch
COPY --from=0 /go/hello .
CMD ["./hello"]Using this approach reduces a Go image from ~1 GB to a few megabytes.
Avoid Unnecessary Docs or Caches
Disable package manager caches (e.g., pip install --no-cache-dir).
Skip documentation installation (e.g., dnf install --nodocs).
Avoid storing package index caches; clean them after installation (e.g., yum makecache).
Clean Up Unneeded Files
Delete temporary files in the same RUN layer where they are created to prevent them from persisting in later layers.
RUN dnf install -y --nodocs <PACKAGES> \
&& dnf clean all \
&& rm -rf /var/cache/dnf RUN apt-get update \
&& apt-get install -y <PACKAGES> \
&& rm -rf /var/lib/apt/lists/*Merge Layers (Squash)
Use the experimental --squash flag with docker build to combine all layers into a single one, eliminating copy‑on‑write overhead.
$ docker build -t squash-image --squash -f Dockerfile .
$ docker history squash-image
IMAGE CREATED CREATED BY SIZE COMMENT
55ded888 9 hours ago # (nop) ENTRYPOINT ["/bin/ash"] 0B
...Note that squashing removes layer reuse benefits for downstream images.
Modify File Metadata While Copying
Instead of a separate COPY followed by RUN chmod/chown, use the --chmod and --chown options (requires BuildKit) to apply metadata in the same layer.
COPY --chmod=755 --chown=normal:normal output/hello /usr/bin/helloWhen using ADD with remote URLs, --chmod may not work; a RUN wget && chmod fallback is needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
