Reduce Docker Image Size by 80% with Multi-Stage Builds: A Guide

Akhilesh BamaneAkhilesh Bamane
2 min read

Introduction

When I first containerized my app, the Docker image size was massive — over 1GB! It worked, but wasn’t production-friendly. That’s when I discovered Multi-Stage Docker Builds — a simple technique that significantly reduced my image size and deployment time.

In this blog, I’ll explain what multi-stage builds are, why you should care and how to use them with real examples and tips.

What is Multi-Stage Build?

Multi-stage builds allow you to use multiple FROM statements in a single Dockerfile. Each stage has a specific purpose.

You can:

  • Use one stage to build your application

  • Use a second (final) stage to run only the necessary output
    This avoids including unnecessary tools, source files, or dev dependencies in the final image.

Why Use It?

Here’s why multi-stage builds are powerful:

  • Reduce image size (significantly)

  • Speed up deployments

  • Improve security (fewer attack surfaces)

  • Clean, production-ready runtime

Real-World Example (React App)

# Stage 1: Builder
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

# Stage 2: Runtime
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

With this setup:

  • All build tools stay in stage 1

  • Final image only contains production-ready static files and NGINX

Result?
My image went from 1.2GB → 250MB
Also, deployments were faster and cleaner.

Tips That Worked for Me

  • Use .dockerignore to skip unnecessary files

  • Don’t copy node_modules into the image

  • Choose lightweight base images (alpine saves hundreds of MBs)

  • Use meaningful stage names (like builder, test, release)

Visual Representation

Bonus: Compare Image Sizes

ApproachImage SizeNotes
Traditional1.2 GBIncludes build tools
Multi-Stage250 MBProduction-ready only

Conclusion

Multi-stage builds are easy to implement and offer big benefits. If you’re using Docker, give it a try — your CI/CD pipeline and ops team will thank you.

References

1
Subscribe to my newsletter

Read articles from Akhilesh Bamane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akhilesh Bamane
Akhilesh Bamane