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

Category: Docker

Docker

Docker: Use Multi-Stage Builds to Reduce Image Size by 90%

- 30.03.26 - ErcanOPAK comment on Docker: Use Multi-Stage Builds to Reduce Image Size by 90%

📦 Tiny Production Images 1.5GB Docker image? Build tools in production? Security nightmare. Multi-stage builds create small, secure images. Only runtime files shipped. The Problem: Bloated Images # ❌ Bad: Everything in one stage FROM node:18 WORKDIR /app # Install dependencies COPY package*.json ./ RUN npm install # Installs dev dependencies too! # Copy source […]

Read More
Docker

Docker: Add Health Checks to Detect and Restart Failing Containers

- 22.03.26 - ErcanOPAK comment on Docker: Add Health Checks to Detect and Restart Failing Containers

💓 Monitor Container Health Automatically Container running but app crashed? Database connection dead? Health checks detect it and restart automatically. The Problem Without Health Checks Container Status: Running ✓ App Status: Crashed ✗ Docker thinks everything is fine. Users see 502 errors. You don’t know until customers complain. Add Health Check in Dockerfile FROM node:18 […]

Read More
Docker

Docker: Use .dockerignore to Speed Up Builds 10x

- 19.03.26 - ErcanOPAK comment on Docker: Use .dockerignore to Speed Up Builds 10x

🚀 Stop Copying Unnecessary Files Docker copying 2GB node_modules to build context? .dockerignore file fixes this. Without .dockerignore: Docker copies ENTIRE directory to build context. With .dockerignore: Copies only what you need. Essential .dockerignore # .dockerignore file node_modules npm-debug.log dist build .git .env .DS_Store *.md .vscode .idea coverage __pycache__ *.pyc .pytest_cache ⚡ Impact Before Build […]

Read More
Docker

Docker: Use docker system prune to Reclaim Disk Space

- 19.03.26 | 19.03.26 - ErcanOPAK comment on Docker: Use docker system prune to Reclaim Disk Space

Docker using 50GB disk space. Old images, stopped containers, unused volumes everywhere. # Remove everything unused (safe) docker system prune -a # Also remove volumes (careful!) docker system prune -a –volumes # See what will be removed first docker system df What Gets Removed: – Stopped containers – Unused images – Dangling build cache – […]

Read More
Docker

Docker: Use –rm Flag to Auto-Delete Stopped Containers

- 19.03.26 - ErcanOPAK comment on Docker: Use –rm Flag to Auto-Delete Stopped Containers

🧹 Self-Cleaning Containers 500 stopped containers eating disk space? One flag prevents this. # Auto-delete after stop docker run –rm -it ubuntu bash # Without –rm (default) docker run -it ubuntu bash # Container stays after exit (uses disk space) # Clean existing stopped containers docker container prune -f When to Use –rm: Testing, one-off […]

Read More
Docker

Docker: Why OrbStack is the New King of Local Containers

- 01.03.26 - ErcanOPAK comment on Docker: Why OrbStack is the New King of Local Containers

Docker Desktop is heavy and slow on Mac/Windows. Enter OrbStack: a lightning-fast alternative. Why switch? 🚀 2x faster network performance. 🔋 Uses 80% less CPU during idle. ⚡ Starts in under 2 seconds. It’s 100% compatible with the docker CLI but optimized for the modern developer’s machine.

Read More
Docker

Docker: Shipping to ARM and x64 Simultaneously with Buildx

- 01.03.26 - ErcanOPAK comment on Docker: Shipping to ARM and x64 Simultaneously with Buildx

With Apple Silicon and ARM-based cloud servers (Graviton) taking over, your Docker images must be multi-arch. docker buildx build –platform linux/amd64,linux/arm64 -t my-app:latest –push . Why? This ensures your container runs natively on everything from a Raspberry Pi to a giant Xeon server without emulation overhead.

Read More
Docker

Docker: Implementing the HEALTHCHECK Instruction for Self-Healing Containers

- 01.03.26 - ErcanOPAK comment on Docker: Implementing the HEALTHCHECK Instruction for Self-Healing Containers

A container can be ‘Running’ but actually ‘Dead’ (e.g., app crashed but process remains). Use the native Docker Healthcheck. HEALTHCHECK –interval=30s –timeout=3s \ CMD curl -f http://localhost/health || exit 1 This allows orchestrators like Swarm or K8s to know exactly when to restart a zombie container, ensuring 99.9% uptime.

Read More
Docker

Docker: Why You Should COPY package.json BEFORE the Rest of Your App

- 28.02.26 - ErcanOPAK comment on Docker: Why You Should COPY package.json BEFORE the Rest of Your App

Are your Docker builds taking too long? You are probably breaking the Layer Cache. # WRONG COPY . . RUN npm install # PRO WAY COPY package*.json ./ RUN npm install COPY . . Why? If you copy everything first, any small code change invalidates the npm install layer. By copying only the JSON first, […]

Read More
Docker

Docker: Reducing Image Size from 1GB to 100MB with Multi-Stage Builds

- 28.02.26 - ErcanOPAK comment on Docker: Reducing Image Size from 1GB to 100MB with Multi-Stage Builds

Why ship the SDK and build tools to production? You only need the compiled binaries. FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /app COPY . . RUN dotnet publish -c Release -o out FROM mcr.microsoft.com/dotnet/aspnet:8.0 WORKDIR /app COPY –from=build /app/out . ENTRYPOINT [“dotnet”, “MyApp.dll”] The Result: Faster deployments, smaller attack surface, and lower storage costs.

Read More
Docker

Docker: Reclaiming GBs of Space with One Command

- 28.02.26 - ErcanOPAK comment on Docker: Reclaiming GBs of Space with One Command

Docker builds create ‘Dangling Images’—layers that are no longer used but take up massive disk space. The Magic Command: docker system prune -a –volumes This removes all unused containers, networks, and images. It’s the digital equivalent of power-washing your development environment.

Read More
Docker

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

- 23.02.26 | 23.02.26 - ErcanOPAK comment on Docker Multi-Stage Builds: From 1.2GB to 50MB – A Production Story

$ 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, […]

Read More
Docker

Docker: Use Build Cache Effectively with Layer Ordering

- 22.02.26 - ErcanOPAK comment on Docker: Use Build Cache Effectively with Layer Ordering

Dockerfile layer order dramatically affects build speed. Put changing layers last, stable layers first. Bad (Slow Rebuilds): FROM node:18 COPY . /app RUN npm install Every code change invalidates npm install cache! Good (Fast Rebuilds): FROM node:18 WORKDIR /app COPY package*.json ./ RUN npm install # Cached unless package.json changes! COPY . . # Code […]

Read More
Docker

Docker: Enhancing Security with Rootless Containers

- 21.02.26 - ErcanOPAK comment on Docker: Enhancing Security with Rootless Containers

Running Docker as ‘root’ is a significant security risk. If a container is compromised, the attacker has root access to the host. Rootless Mode mitigates this risk. “Security isn’t about one big wall; it’s about layers of protection. Rootless Docker is a crucial layer.” Rootless mode allows running the Docker daemon and containers as a […]

Read More
Docker

Docker: Stop Guessing if Your Container is Truly ‘Healthy’

- 21.02.26 - ErcanOPAK comment on Docker: Stop Guessing if Your Container is Truly ‘Healthy’

A container can be ‘running’ but the app inside could be crashed or stuck in a loop. Use native Docker Healthchecks. HEALTHCHECK –interval=30s –timeout=3s \ CMD curl -f http://localhost/health || exit 1 Now, docker ps will show (healthy) or (unhealthy) instead of just ‘Up’.

Read More
Docker

Docker: Optimizing Layer Cache for Lightning Fast CI/CD Builds

- 21.02.26 - ErcanOPAK comment on Docker: Optimizing Layer Cache for Lightning Fast CI/CD Builds

Every line in a Dockerfile is a layer. If you change a file, all subsequent layers are rebuilt. This is slow. The Pro Strategy: Always copy your dependency files (package.json, .csproj) BEFORE your source code. This ensures that ‘npm install’ or ‘dotnet restore’ is only re-run if dependencies change, not every time you edit a […]

Read More
Docker

Docker: Shrink Image Size by 80% with Multi-Stage Builds

- 21.02.26 - ErcanOPAK comment on Docker: Shrink Image Size by 80% with Multi-Stage Builds

Don’t include the SDK and build tools in your production image. Use a multi-stage approach to only ship the final binary. FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src COPY . . RUN dotnet publish -c Release -o /app FROM mcr.microsoft.com/dotnet/aspnet:8.0 WORKDIR /app COPY –from=build /app . ENTRYPOINT [“dotnet”, “MyApp.dll”]

Read More
Docker

Docker: Use .dockerignore to Reduce Build Context and Speed Up Builds

- 21.02.26 - ErcanOPAK comment on Docker: Use .dockerignore to Reduce Build Context and Speed Up Builds

Sending entire project folder to Docker slows builds. .dockerignore excludes unnecessary files. Create .dockerignore in project root: # Version control .git .gitignore # Dependencies (will be installed in container) node_modules npm-debug.log vendor/ # Build outputs dist/ build/ *.log # IDE files .vscode/ .idea/ *.swp # Testing coverage/ .nyc_output/ # OS files .DS_Store Thumbs.db # Docs […]

Read More
Docker

Docker: Use Compose Profiles to Start Only Needed Services

- 17.02.26 - ErcanOPAK comment on Docker: Use Compose Profiles to Start Only Needed Services

Running all services (database, cache, queue, monitoring) every time is slow. Profiles start only what you need. docker-compose.yml: services: app: build: . ports: – “3000:3000” db: image: postgres profiles: [“dev”, “test”] redis: image: redis profiles: [“dev”] monitoring: image: grafana profiles: [“monitoring”] mailhog: image: mailhog/mailhog profiles: [“dev”] Start Different Profiles: # Just the app (no extras) […]

Read More
Docker

Docker: Use ARG for Build-Time Variables and ENV for Runtime

- 16.02.26 - ErcanOPAK comment on Docker: Use ARG for Build-Time Variables and ENV for Runtime

Confusing ARG and ENV causes build issues. Know when to use each. ARG – Build-Time Only: FROM node:18 ARG NODE_ENV=production ARG API_URL=https://api.example.com RUN echo “Building for ${NODE_ENV}” RUN npm install –${NODE_ENV} # ARG values NOT available at runtime! Build with ARG: docker build –build-arg NODE_ENV=development -t myapp . ENV – Runtime Variables: FROM node:18 ENV […]

Read More
Docker

Docker: Use Docker Init to Generate Dockerfiles Automatically

- 15.02.26 - ErcanOPAK comment on Docker: Use Docker Init to Generate Dockerfiles Automatically

Writing Dockerfiles from scratch is error-prone. Docker init analyzes your project and generates optimized Dockerfile. Run in Project Directory: docker init # Detects project type (Node.js, Python, Go, etc.) # Asks questions about your app # Generates: # – Dockerfile # – .dockerignore # – docker-compose.yml Example Output for Node.js: Creates multi-stage build, sets up […]

Read More
Docker

Docker: Use Multi-Stage Builds to Reduce Final Image Size by 90%

- 15.02.26 - ErcanOPAK comment on Docker: Use Multi-Stage Builds to Reduce Final Image Size by 90%

Including build tools in final image wastes space. Multi-stage builds separate build and runtime. Single-Stage (Large): FROM node:18 WORKDIR /app COPY . . RUN npm install # Includes dev dependencies RUN npm run build CMD [“node”, “dist/server.js”] # Result: 1.2 GB (includes npm, build tools) Multi-Stage (Small): # Stage 1: Build FROM node:18 AS builder […]

Read More
Docker

Docker: UseHealthCheck to Auto-Restart Unhealthy Containers

- 14.02.26 - ErcanOPAK comment on Docker: UseHealthCheck to Auto-Restart Unhealthy Containers

Container running but app inside crashed? Add HEALTHCHECK to auto-detect and restart failed containers. Add to Dockerfile: FROM node:18 WORKDIR /app COPY . . # Check if app responds on port 3000 HEALTHCHECK –interval=30s –timeout=3s –retries=3 \ CMD curl -f http://localhost:3000/health || exit 1 CMD [“node”, “server.js”] What Happens: Every 30 seconds, Docker runs health […]

Read More
Docker

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

- 13.02.26 - ErcanOPAK comment on Docker: Use .dockerignore to Speed Up Builds and Reduce Image Size

Copying node_modules, .git, and cache files into images wastes time and space. Exclude them with .dockerignore. Create .dockerignore in project root: node_modules npm-debug.log .git .gitignore *.md .env .vscode .idea **/*.log **/dist **/bin **/obj Impact: Without .dockerignore: – Build time: 45 seconds – Image size: 850 MB (includes node_modules) With .dockerignore: – Build time: 8 seconds […]

Read More
Docker

Docker: Use docker compose watch to Auto-Rebuild on File Changes

- 13.02.26 - ErcanOPAK comment on Docker: Use docker compose watch to Auto-Rebuild on File Changes

Manually rebuilding Docker containers after every code change? Docker Compose watch auto-rebuilds on file save. Add to docker-compose.yml: services: web: build: . develop: watch: – path: ./src action: rebuild Start with watch: docker compose watch Edit src/ files → Docker auto-rebuilds container. No manual restart needed! Actions: rebuild (full rebuild), sync (copy files), sync+restart (copy […]

Read More
Docker

Docker Compose: Launch Full Stack Apps with One Command (Node + Redis + Postgres)

- 05.02.26 - ErcanOPAK comment on Docker Compose: Launch Full Stack Apps with One Command (Node + Redis + Postgres)

Manually starting 5 different services for development? Docker Compose defines and runs multi-container apps. # docker-compose.yml version: ‘3.8’ services: # Node.js API api: build: ./api ports: – “3000:3000” environment: – NODE_ENV=development – REDIS_URL=redis://redis:6379 – DATABASE_URL=postgresql://user:pass@db:5432/mydb volumes: – ./api:/app – /app/node_modules depends_on: – redis – db command: npm run dev # React Frontend frontend: build: ./frontend […]

Read More
Docker

Docker: Clean Up Disk Space by Removing Unused Images and Containers

- 03.02.26 - ErcanOPAK comment on Docker: Clean Up Disk Space by Removing Unused Images and Containers

Docker eating 50GB of disk space? Old images, stopped containers, and dangling volumes pile up fast. One command reclaims it all. Check Current Disk Usage: docker system df # Output: TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 47 12 28.5GB 18.2GB (63%) Containers 89 5 10.2GB 9.8GB (96%) Local Volumes 23 8 2.3GB 1.1GB (47%) Build […]

Read More
Docker

Reduce Docker Image Sizes by 10x with Multi-Stage Builds

- 01.02.26 | 01.02.26 - ErcanOPAK comment on Reduce Docker Image Sizes by 10x with Multi-Stage Builds

Your Docker image is 1.2GB when it should be 120MB? Multi-stage builds eliminate build dependencies from your final image. The Problem – Single Stage Build: FROM node:18 WORKDIR /app COPY package*.json ./ RUN npm install # Installs 300MB of dev dependencies COPY . . RUN npm run build # Creates 10MB production build CMD [“node”, […]

Read More
Docker

Why Your Docker Image Is Slow Even Though It’s “Small”

- 31.01.26 - ErcanOPAK comment on Why Your Docker Image Is Slow Even Though It’s “Small”

Image size ≠ image performance. Hidden cause Layer invalidation. Most Dockerfiles are written like this: COPY . . RUN npm install Every small file change invalidates the cache. Correct approach COPY package.json package-lock.json ./ RUN npm install COPY . . Cause → Effect Stable dependency layers Faster rebuilds Lower CI/CD costs This is why two […]

Read More
Docker

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

- 30.01.26 - ErcanOPAK comment on Why Docker Containers Randomly Slow Down After Days (Even With Low Traffic)

If a container runs for days, performance can degrade without any load increase. What actually happens Linux page cache grows but isn’t reclaimed fast enough Log files inside containers expand silently OverlayFS layers fragment over time Golden principle Containers are ephemeral by design, not meant to be immortal. Fix Externalize logs Restart containers on a […]

Read More
Page 1 of 2
1 2 Next »

Posts navigation

Older posts
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 (859)
  • 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 (754)
  • 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 (448)

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 (859)
  • 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 (754)

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