Optimizing Images
The Global Marketplace
Imagine packing for a trip. If you throw everything into your suitcase - clothes, books, gadgets - it becomes heavy, slow to carry, and expensive to check in. But if you pack only what you need, fold neatly, and use compartments wisely, your suitcase is light, fast, and efficient.
Docker images are exactly like suitcases. Bloated images slow down builds, consume bandwidth, and waste storage. Optimized images are lean, portable, and production‑ready.
Optimization Foundations
1. Why Optimize Images?
- Performance: Smaller images pull faster and start containers quicker.
- Efficiency: Reduced disk usage and network bandwidth.
- Security: Fewer packages mean fewer vulnerabilities.
- Portability: Easier to distribute across environments.
2. Techniques for Optimization
- Use Lightweight Base Images:
- Prefer
alpineorscratchover heavy OS images likeubuntu.
- Prefer
- Multi‑Stage Builds:
- Build in stages, keeping only the final artifacts.
- Minimize Layers:
- Combine commands to reduce unnecessary layers.
- Clean Up After Installations:
- Remove caches, temporary files, and unused dependencies.
- Pin Versions:
- Specify versions to avoid unexpected changes.
- Security Scanning:
- Regularly scan images for vulnerabilities.
3. Example: Multi‑Stage Build
# Stage 1: Build the application
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Stage 2: Create a lean runtime image
FROM alpine:latest
COPY --from=builder /app/myapp /myapp
CMD ["./myapp"]
- The final image contains only the compiled binary, not the entire build environment.
4. Comparing Image Sizes
ubuntu:20.04→ ~70MB.alpine:latest→ ~5MB.- Optimized images can be 10x smaller, saving time and resources.
Things to Remember
- Optimized images are faster, safer, and easier to distribute.
- Multi‑stage builds are the most powerful optimization technique.
- Always clean up unnecessary files and dependencies.
Hands‑On Lab
Step 1: Build a Simple Image with Ubuntu
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y curl
CMD ["curl", "--version"]
Build and run:
docker build -t ubuntu-curl .
docker run ubuntu-curl
Step 2: Build the Same Image with Alpine
FROM alpine:latest
RUN apk add --no-cache curl
CMD ["curl", "--version"]
Build and run:
docker build -t alpine-curl .
docker run alpine-curl
Step 3: Compare Sizes
docker images
- Notice how much smaller the Alpine‑based image is compared to Ubuntu.
Practice Exercise
- Create a Dockerfile that installs
python3and runs a simple script. - Build it using
ubuntuas the base image. - Rebuild it using
alpineas the base image. - Compare build times and image sizes.
- Reflect on why the optimized image is better for production.
Visual Learning Model
Dockerfile → Build Layers → Multi-Stage Build → Optimized Image → Faster Deployment
The Hackers Notebook
Optimizing Docker images is about packing smart. By using lightweight bases, multi‑stage builds, minimizing layers, and cleaning up, developers create lean, secure, and efficient images. Optimized images save time, reduce costs, and improve reliability in production environments.
