10 Proven Techniques to Shrink Docker Image Size for Faster CI/CD
This guide explains why Docker image size matters for CI/CD and cloud deployments, then details ten practical methods—including layer minimization, multi‑stage builds, slim base images, apt flags, .dockerignore, and specialized tools—complete with code snippets and visual comparisons to help you create leaner containers.
What is Docker?
Docker is a container engine that runs applications in isolated environments. Images are built from a Dockerfile containing instructions such as FROM, RUN, COPY, EXPOSE, etc.
Why Reduce Docker Image Size?
Unnecessary packages increase the attack surface.
Larger images take longer to transfer.
Deploying large images consumes more time.
Techniques to Reduce Docker Image Size
1. Minimize Image Layers
Combine multiple commands into a single RUN to reduce the number of layers.
FROM ubuntu:latest
RUN apt update -y && \
apt install -y unzip curl python3 --no-install-recommends && \
rm -rf /var/lib/apt/lists/*2. Use Docker Squash
Install docker-squash and squash layers into a single layer.
pip install docker-squash
docker-squash image:old -t image:new3. Choose Smaller Base Images
Prefer slim or Alpine variants. Example sizes: python:3.9 ≈ 1.3 GB, python:3.9-slim ≈ 1 GB, python:3.9-alpine ≈ 49 MB.
4. Multi‑Stage Builds
Separate build and runtime stages so that only the final artifacts are copied into the runtime image.
# Stage 1 – build
FROM node:14.17-alpine3.14 AS build
COPY public /home/app/public/
COPY src /home/app/src/
RUN apk add --no-cache g++ make python2 \
&& npm install --silent \
&& npm run build \
&& apk del python2
# Stage 2 – runtime
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;"]5. Install Packages Without Recommended Extras
Use --no-install-recommends (apt) or --no-cache (apk) to avoid unnecessary packages.
FROM ubuntu:latest
RUN apt update -y && \
apt install -y unzip curl python3 --no-install-recommends && \
rm -rf /var/lib/apt/lists/*6. Clean Package Manager Cache
Delete the apt cache after installation to free space.
RUN rm -rf /var/lib/apt/lists/*7. Use a .dockerignore File
Exclude files that are not needed in the build context.
ignorethisfile.txt
logs/
ignorethisfolder/
.git
.cache
*.md8. Order COPY After Heavy RUN Steps
Placing COPY after RUN allows Docker to cache the expensive steps and rebuild faster.
# Better
FROM ubuntu:latest
RUN apt update -y && \
apt install -y unzip curl python3 --no-install-recommends && \
rm -rf /var/lib/apt/lists/*
COPY file /home/ubuntu
# Worse
FROM ubuntu:latest
COPY file /home/ubuntu
RUN apt update -y && \
apt install -y unzip curl python3 --no-install-recommends && \
rm -rf /var/lib/apt/lists/*9. Delete Installation Artifacts
Remove downloaded archives after they are used.
FROM ubuntu:latest
RUN curl -L "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o awscliv2.zip && \
unzip awscliv2.zip && \
./aws/install && \
rm awscliv2.zip10. Analyse Image Size with Tools
Tools such as Dive (https://github.com/wagoodman/dive), fromlatest.io (https://www.fromlatest.io/), and Docker Slim (https://github.com/slimtoolkit/slim) can inspect layers and suggest further reductions.
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.
