DEV Community

Anusha Kuppili
Anusha Kuppili

Posted on

Shrink Your Docker Image by 60% Using Multi-Stage Builds and Distroless Containers

Hey Devs! ๐Ÿ‘‹

Iโ€™m Anusha, the voice behind Data Enthusiast Era โ€” a channel where I simplify DevOps, Data Science, and MLOps, especially for folks getting started.

Recently, I managed to reduce a Docker image from 146MB to just 57MB.

No tricks. Just Multi-Stage Docker Builds and Distroless Containers.

Hereโ€™s how I did it โ€” explained in a way that actually makes sense if you're new to Docker or DevOps.


๐Ÿณ The Problem: Docker Images Get Bloated Fast

When we use official base images like python:3.11 or node, they include:

  • a shell
  • compilers
  • package managers
  • other stuff you donโ€™t need at runtime

This makes the image:

  • Bigger
  • Slower to deploy
  • More vulnerable

โœจ The Solution: Multi-Stage Builds

With Multi-Stage Builds, we split the Dockerfile into two parts:

  1. One for building (with all tools needed)
  2. One for running (with only the app and its dependencies)

Itโ€™s like packing light for production โ€” just what you need to survive!


๐Ÿ”’ Bonus: Use Distroless Images

Distroless images are minimal base images built by Google.

They donโ€™t include:

  • /bin/bash
  • a shell
  • any package manager

Which means:

  • โœจ Smaller
  • ๐Ÿ” More secure
  • โšก Faster to run

โš™๏ธ My Real Example (Python App)

Step 1: Traditional Dockerfile

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›‘ Image size: 146MB

Step 2: Multi-Stage + Distroless

FROM python:3.11-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --target=/install -r requirements.txt
COPY . .

FROM gcr.io/distroless/python3
COPY --from=builder /install /usr/local/lib/python3.11/site-packages
COPY --from=builder /app /app
WORKDIR /app
CMD ["main.py"]
Enter fullscreen mode Exit fullscreen mode

โœ… Image size: 57.8MB

๐Ÿ“Š Result:
Build Type Image Name Size
Traditional demo-bloat 146MB
Distroless demo-distroless 57.8MB

๐ŸŽ‰ Thatโ€™s a 60% image size reduction โ€” in production, thatโ€™s a huge win.

๐Ÿ” Why This Matters
โšก Pull/push images faster

๐Ÿ” Reduce attack surface

๐Ÿงน Avoid leftover tools and clutter

๐Ÿ’ธ Save cloud storage and bandwidth

๐ŸŽฅ Watch the Full Demo
Want to see me explain and run this live?
โ–ถ๏ธ Watch the full video on YouTube

๐Ÿ™Œ Final Thoughts
If youโ€™re starting out with Docker or DevOps, donโ€™t wait to learn about multi-stage builds โ€” itโ€™s a game changer.

Iโ€™m Anusha Kuppili โ€” a woman in tech and the creator of Data Enthusiast Era, where I make tech concepts simple, visual, and fun to learn.

Let me know in the comments:
๐Ÿ‘‰ How big is your Docker image right now?
Letโ€™s shrink it together!

๐Ÿ’ก Follow me on:
๐ŸŽฅ YouTube: https://www.youtube.com/@DataEnthusiastEra

Top comments (0)