10 Proven Ways to Shrink Your Docker Image Size
This guide explains why smaller Docker images improve security, transfer speed, and deployment time, and walks through ten practical techniques—including layer minimization, Docker‑squash, slim base images, multi‑stage builds, apt flags, .dockerignore, and specialized tooling—to reliably reduce image size.
Docker is a container engine that runs code in isolated environments, and Docker images package applications with all their dependencies. Large images increase attack surface, slow transfer, and prolong deployment, so optimizing image size is essential.
1. Minimize Image Layers
Each FROM, RUN, and COPY instruction creates a separate layer, inflating the image. Combine related commands into a single RUN or COPY to reduce layers.
RUN apt install unzip -y && \
apt install python3 -yCombining commands can save several megabytes, as shown in the accompanying diagram.
2. Use Docker‑Squash
Docker creates many layers during a build; squashing merges them into a more compact structure. pip install docker-squash Then run:
docker-squash image:old -t image:new3. Choose Smaller Base Images
Switch from full images to slim or Alpine variants. For Python, python:3.9-slim (~1 GB) is much smaller than python:3.9 (~1.3 GB), and python:3.9-alpine is only about 49 MB.
4. Multi‑Stage Builds
Separate build and runtime stages, copying only the necessary artifacts to the final image.
# Official build image
FROM node:14.17-alpine3.14 as build
COPY public /home/app/public/
RUN apk add g++ make python2 && \
apk --purge del python2
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /home/app/build /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]5. Use --no-install-recommends with apt
Prevent installation of unnecessary recommended packages.
apt install unzip -y --no-install-recommends && \
apt install curl -y --no-install-recommends && \
apt install python3 -y --no-install-recommends6. Clean APT Cache After Installation
Remove the package list to free space.
apt install unzip -y --no-install-recommends && \
apt install curl -y --no-install-recommends && \
apt install python3 -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*7. Use a .dockerignore File
Exclude unnecessary files (e.g., .git, docs) from the build context to keep the image lean.
8. Place COPY After RUN When Possible
Doing so improves cache reuse, reducing rebuild time and image size.
apt install unzip -y --no-install-recommends && \
apt install curl -y --no-install-recommends && \
apt install python3 -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*9. Delete Installation Artifacts
Remove downloaded archives after installing packages, such as the AWS CLI zip file.
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
unzip awscliv2.zip -d /tmp && \
./aws/install && \
rm -rf awscliv2.zip /tmp/aws10. Use Image‑Sizing Tools
Tools like Dive , fromlatest.io , and Docker Slim analyze Dockerfiles and images to suggest further reductions.
https://github.com/wagoodman/dive https://www.fromlatest.io/ https://github.com/slimtoolkit/slimApplying these ten methods can dramatically shrink Docker images, improve security, and speed up deployments.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
