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)