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: Understand Image Layers for Efficient Builds

- 21.06.26 - ErcanOPAK

πŸ“š Each Dockerfile Line = One Layer

Docker images are layers. Understanding layers helps optimize builds. Cache, reuse, smaller images.

πŸ“ How Layers Work

Dockerfile:
FROM node:18           # Layer 1: Base image
WORKDIR /app           # Layer 2: Set working directory
COPY package*.json ./  # Layer 3: Copy package files
RUN npm install        # Layer 4: Install dependencies
COPY . .               # Layer 5: Copy source
RUN npm run build      # Layer 6: Build
CMD ["npm", "start"]  # Layer 7: Command

# Each layer is cached
# Changing layer 5 invalidates layers 5, 6, 7
# But layers 1-4 are reused (cached)

# View layers
docker history myapp:latest

# Optimize: Put frequently changed files LAST
COPY package*.json ./  # Changes rarely
RUN npm install        # Cached
COPY . .               # Changes often (last)

🎯 Layer Optimization Tips

# 1. Combine RUN commands
RUN apt-get update && apt-get install -y \
    git \
    curl \
    wget \
    && rm -rf /var/lib/apt/lists/*

# 2. Use multi-stage builds
FROM node:18 AS builder
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

# 3. Leverage cache
COPY package*.json ./
RUN npm install
COPY . .  # Changes here doesn't invalidate npm install layer

# 4. Use .dockerignore
# Exclude node_modules, .git, etc. from build context

# 5. Use smaller base images
# node:18-alpine (170MB) vs node:18 (950MB)

πŸ’‘ Commands

  • docker history image:tag (show layers)
  • docker build –no-cache (rebuild without cache)
  • docker image prune -a (remove unused layers)
  • docker system df (check disk usage by layers)

“Build took 5 minutes each time. Reordered layers, used cache. Build time dropped to 30 seconds. Understanding layers is essential for Docker.”

β€” DevOps Engineer

Related posts:

Docker Containers Work Locally but Fail in CI

Docker: Use .dockerignore to Exclude Unnecessary Files

Why Your Docker Image Is Slow Even Though It’s β€œSmall”

Post Views: 2

Post navigation

Kubernetes: Use Service Accounts for Pod Authentication
AI Prompt: Generate Project Risk Assessment

Leave a Reply Cancel reply

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

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

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

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

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

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

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