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)