๐ณ Day 5: Docker Multi-Stage Builds & Image Optimization
Video Overview
This note explains the concept of Docker Multi-Stage Builds, how they help optimize image sizes, and why separating build and runtime environments is critical for production-ready containers.
๐ Core focus:
- Build vs Runtime separation
- Image size optimization
- Practical Dockerfile implementation
๐ Concept 1: Build vs Runtime (Pizza Analogy)
Quote
Anything required to build the application should NOT be part of the final image.
Think of making a pizza:
- Build stage โ Ingredients, tools (microwave, measuring cup)
- Runtime stage โ Eating the pizza ๐
๐ง Key Idea
- Build tools โ Runtime requirements
- Final image should only contain what is needed to run the app
๐ Concept 2: Why Smaller Images Matter
Important
Smaller Docker images are faster, safer, and more efficient.
๐ Benefits:
- โก Faster pull & push
- ๐ Faster deployment
- ๐ Reduced attack surface (more secure)
- ๐พ Less storage usage
โ๏ธ Concept 3: Compiled vs Interpreted Languages
Why this matters?
Determines whether your app needs a build stage
๐งฑ Compiled Languages
- Require conversion to machine code
- Need build tools (compilers)
- Examples:
- Java
- C / C++
- Go
๐งพ Interpreted Languages
- Run line-by-line via interpreter
- No compilation required
- Examples:
- Python
- JavaScript
- PHP
๐ง Takeaway
- Compiled โ Heavy build stage required
- Interpreted โ Usually no build stage (but can still use multi-stage)
๐ Concept 4: Example Application (Python Flask)
Application Details
- Framework: Flask
- Port: 5000
- Output:
Hello Docker
๐ Single Stage Dockerfile
FROM python:3.9
WORKDIR /app
COPY app.py /app
RUN pip install flask
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]๐ง Improvements in Dockerfile
Tip
Follow best practices for cleaner Dockerfiles
- Use
COPYinstead ofADD - Use
ENTRYPOINT + CMDtogether
โ ๏ธ Problem: Unnecessary Build Tools
apt update
apt install -y build-essentialWarning
Build tools increase image size unnecessarily
๐ฆ Result
- Image size: ~495 MB
๐๏ธ Concept 5: Multi-Stage Dockerfile
Multi-Stage Build
# Stage 1: Build Stage
FROM python:3.9 AS build
WORKDIR /app
COPY app.py .
RUN apt update && apt install -y build-essential
RUN pip install flask
# Stage 2: Final Runtime Stage
FROM python:3.9
WORKDIR /app
COPY --from=build /app .
RUN pip install flask
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]๐ Key Instruction
COPY --from=build /app .Important
Copies only required files from build stage โ excludes heavy tools
๐ Concept 6: Size Comparison
Optimization Result
| Image Type | Size |
|---|---|
| Single Stage | 495 MB |
| Multi-Stage | 162 MB |
๐ฅ Result
- ~67% size reduction
- Same functionality โ
๐งช Running Containers
docker run -p 8080:5000 python-image
docker run -p 8081:5000 python-image-multi๐ Output
localhost:8080โ Hello Dockerlocalhost:8081โ Hello Docker
๐ง Final Takeaways
Summary
- Multi-stage builds separate build & runtime
- Remove unnecessary dependencies
- Reduce image size drastically
- Improve security & performance
๐ One-Line Summary
Quote
Build heavy โ Ship light ๐