📦 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 […]
Category: Docker
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 […]
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 […]
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 – […]
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 […]
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.
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.
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.
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, […]
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.
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.
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, […]
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 […]
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 […]
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’.
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 […]
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”]
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 […]
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) […]
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 […]
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 […]
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 […]
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 […]
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 […]
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 […]
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 […]
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 […]
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”, […]
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 […]
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 […]























