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:
- One for building (with all tools needed)
- 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"]
π 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"]
β 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)