10 Proven Techniques to Shrink Your Docker Image Size
This article presents ten practical methods—including minimizing layers, using smaller base images, multi‑stage builds, and specialized slimming tools—to significantly reduce Docker image size, improve build speed, and lower security risks.
In this article we explore methods to reduce Docker image size.
What is Docker?
Docker is a container engine that runs code in isolated containers. A Docker image packages an application and its dependencies so it can run anywhere.
To build an image Docker uses a Dockerfile containing instructions such as RUN, COPY, EXPOSE, etc. When these commands are executed Docker creates an image for later use.
Why shrink Docker images?
Installing unnecessary packages increases the attack surface and security risk.
Larger images take longer to transfer.
Deploying large images consumes more time.
We need to write Dockerfiles that produce size‑optimized images. Below are ten effective techniques.
1. Minimize image layers
Each FROM, RUN, COPY creates a separate layer, increasing size and build time. Combine multiple commands in a single RUN or COPY to reduce layers.
FROM ubuntu:latest
RUN apt update -y && apt install unzip -y && apt install curl -y && apt install python3 -yInstead of separate RUN statements, combine them:
FROM ubuntu:latest
RUN apt update -y && apt install unzip -y && \
apt install curl -y && \
apt install python3 -y2. Use Docker Squash
Docker creates many layers during build; squashing can compress and reorganize layers.
Install docker‑squash: pip install docker-squash Then squash an image:
docker-squash image:old -t image:new3. Use a smaller base image
Choosing a smaller base image reduces size dramatically. For Python use python:3.9-slim instead of python:3.9. The slim image is about 1 GB versus 1.3 GB. Alpine images are even smaller; python:3.9-alpine is ~49 MB.
4. Multi‑stage builds
Multi‑stage builds separate build and runtime stages, copying only the necessary artifacts to the final image, which greatly reduces size.
# Official docker build image, using node:14.17-alpine3.14 for stage-1.
# Stage-1
FROM node:14.17-alpine3.14 as build
COPY public /home/app/public/
COPY src /home/app/src/
RUN apk add g++ make python2
RUN npm install --silent
RUN npm run build
RUN apk --purge del python2
# Stage-2
FROM nginx:stable-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /home/app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx","-g","daemon off;"]Stage‑1 builds the application; Stage‑2 runs it with a minimal Nginx image.
5. Use --no-install-recommends with apt
apt installs recommended packages by default, which adds unnecessary size. Adding --no-install-recommends reduces the image.
FROM ubuntu:latest
RUN apt update -y && \
apt install unzip -y --no-install-recommends && \
apt install curl --no-install-recommends -y && \
apt install python3 -y --no-install-recommends6. Remove apt lists after install
Delete /var/lib/apt/lists/* after installing packages to save space.
FROM ubuntu:latest
RUN apt update -y && \
apt install unzip -y --no-install-recommends && \
apt install curl --no-install-recommends -y && \
apt install python3 -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*7. Use a .dockerignore file
Exclude files and directories from the build context to prevent them from being copied into the image.
.dockerignore
ignorethisfile.txt
logs/
ignorethisfolder/
.git
.cache
*.md8. Place COPY after RUN
When you frequently change source code, putting COPY after RUN allows Docker to cache the earlier layers, reducing rebuild time and image size.
# Dockerfile-1 (better)
FROM ubuntu:latest
RUN apt update -y && \
apt install unzip -y --no-install-recommends && \
apt install curl --no-install-recommends -y && \
apt install python3 -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*
COPY file /home/ubuntu
# Dockerfile-2 (worse)
FROM ubuntu:latest
COPY file /home/ubuntu
RUN apt update -y && \
apt install unzip -y --no-install-recommends && \
apt install curl --no-install-recommends -y && \
apt install python3 -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*9. Delete installer files after use
Remove downloaded archives after installing software, e.g., delete the AWS CLI zip after extraction.
FROM ubuntu:latest
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
unzip awscliv2.zip && \
sudo ./aws/install && \
rm awscliv2.zip10. Use Docker image slimming tools
Several tools can automatically analyze and reduce image size, such as Dive, fromlatest.io, and Docker Slim.
Dive – explores image layers and suggests reductions.
fromlatest.io – checks Dockerfiles for further optimizations.
Docker Slim – creates minimal, secure containers.
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.
