Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Docker

Docker Multi-Stage Builds: From 1.2GB to 50MB – A Production Story

- 23.02.26 | 23.02.26 - ErcanOPAK

$ docker images | grep ‘before’

myapp:before    1.2GB    "This is fine" 🔥
myapp:after     50MB     "Wait, what?" 🤯

How we reduced our Docker image by 96% and cut deployment time from 10 minutes to 30 seconds.

The Problem: Build Artifacts in Production

❌ The Bad Old Way

FROM node:18
WORKDIR /app

# Copy everything (including .git, node_modules, tests)
COPY . .

# Install ALL dependencies (including dev dependencies)
RUN npm install

# Build
RUN npm run build

# Now we have:
# - node_modules (500MB)
# - Source code (100MB)
# - Git history (50MB)
# - Tests (30MB)
# - Build tools (200MB)
# Total: 1.2GB 😱

✅ Multi-Stage Build

# Stage 1: Build
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build

# Stage 2: Production
FROM node:18-alpine  # Alpine = tiny base image
WORKDIR /app

# Only copy what we need!
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package.json ./

USER node  # Security: don't run as root!
CMD ["node", "dist/server.js"]

# Final image: 50MB 🎉
# - No source code
# - No dev dependencies
# - No build tools
# - No Git history

📊 Real-World Impact

Metric Before After Improvement
Image Size 1.2 GB 50 MB 96% smaller
Pull Time 5 min 10 sec 30x faster
Deploy Time 10 min 30 sec 20x faster
Registry Costs $200/month $10/month 95% cheaper
Attack Surface High Minimal Much safer
50MB
Final Image Size
30s
Deploy Time
96%
Size Reduction

💡 Pro Tips

  • Use Alpine base images – 5MB vs 150MB for standard images
  • Order matters – Put rarely-changing stuff first (package.json before source code)
  • Use .dockerignore – Like .gitignore but for Docker (exclude tests, docs, .git)
  • Don’t run as root – Add USER node before CMD
  • Multi-stage = free – No performance penalty, only benefits

“Our CI/CD went from timing out to completing in under a minute. Our AWS bill dropped by $150/month just from smaller images. Multi-stage builds paid for themselves in week one.”

— Lead DevOps Engineer, Fintech Startup

Related posts:

Docker: Use .dockerignore to Speed Up Builds and Reduce Image Size

Multi-Stage Builds = Smaller Images

Why Docker Containers Randomly Slow Down After Days (Even With Low Traffic)

Post Views: 6

Post navigation

Kubernetes Monitoring: The Prometheus + Grafana Stack That Saved Our Production
AI Prompt Engineering: The $200/Hour Technique Used by Top Consultants

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (752)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (447)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (752)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com