๐Ÿณ 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 COPY instead of ADD
  • Use ENTRYPOINT + CMD together

โš ๏ธ Problem: Unnecessary Build Tools

apt update
apt install -y build-essential

Warning

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 TypeSize
Single Stage495 MB
Multi-Stage162 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 Docker
  • localhost: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 ๐Ÿš€