dockerfile-slim
v1.0.0
Published
Analyze and optimize Dockerfiles - reduce image size by up to 80%
Downloads
107
Maintainers
Readme
dockerfile-slim
Analyze and optimize Dockerfiles - reduce image size by up to 80%
Problem
Your Docker image is 2GB. Deploys take forever. Registry storage costs money.
Solution
dockerfile-slim analyzes your Dockerfile and provides actionable optimization suggestions with estimated size and cost savings.
Features
- Instant analysis - No Docker build required
- Base image alternatives - Suggests smaller base images (e.g., node → node:alpine)
- Multi-stage build detection - Identifies missing multi-stage opportunities
- Layer optimization - Finds inefficient layer patterns
- Cache optimization - Detects poor layer caching strategies
- Cost estimation - Shows monthly registry cost savings
- Auto-fix - Generates optimized Dockerfiles
- .dockerignore generation - Prevents bloat from unnecessary files
Installation
# Run directly with npx (recommended)
npx dockerfile-slim
# Or install globally
npm install -g dockerfile-slim
# Or as dev dependency
npm install --save-dev dockerfile-slimUsage
Basic Analysis
# Analyze Dockerfile in current directory
npx dockerfile-slim
# Analyze specific Dockerfile
npx dockerfile-slim ./path/to/Dockerfile
# Output as JSON (for CI/CD)
npx dockerfile-slim --jsonGenerate Optimized Dockerfile
# Generate and display optimized Dockerfile
npx dockerfile-slim --fix
# Save to file
npx dockerfile-slim --fix --output Dockerfile.optimized
# Also generate .dockerignore
npx dockerfile-slim --fix --dockerignoreExample Output
📦 Dockerfile Analysis
──────────────────────────────────────────────────
File: ./Dockerfile
Base Image: node:18
Estimated Size: 1.2 GB
Layer Count: 8
Efficiency Score: 35/100
💡 Found 5 optimization opportunities:
🔴 Critical
🔴 Smaller base image available
Switch from node:18 to node:18-alpine
→ Potential savings: 965 MB
→ Cost savings: ~$5.30/month
🔴 Missing multi-stage build (Node.js)
Node.js projects should use multi-stage builds
→ Potential savings: 800 MB
→ Cost savings: ~$4.40/month
🟠 High Priority
🟠 apt-get without cleanup
apt-get install without cleaning cache adds ~50-200MB
Line 5
→ Potential savings: 100 MB
→ Add cleanup in same RUN: && rm -rf /var/lib/apt/lists/*
🟠 Missing .dockerignore
Without .dockerignore, unnecessary files may be included
→ Potential savings: 200 MB
🟡 Medium Priority
🟡 npm cache not cleaned
npm cache can add 50-100MB
→ Potential savings: 50 MB
→ Add npm cache clean --force after install
──────────────────────────────────────────────────
Summary Value
───────────────────────────────────────
Total potential savings 2.0 GB
Estimated cost savings $11.20/month
Potential size reduction 80%
💡 Run with --fix to generate an optimized DockerfileGenerated Dockerfile Example
Running npx dockerfile-slim --fix generates:
# syntax=docker/dockerfile:1
# ---- Build Stage ----
FROM node:20-slim AS builder
WORKDIR /app
# Copy package files first for better layer caching
COPY package.json package-lock.json* ./
# Install ALL dependencies (including devDependencies for build)
RUN npm ci
# Copy source code
COPY . .
# Build the application
RUN npm run build
# ---- Production Stage ----
FROM node:20-slim AS production
# Create non-root user for security
RUN groupadd --gid 1001 nodejs && \
useradd --uid 1001 --gid nodejs --shell /bin/bash --create-home nodejs
WORKDIR /app
# Copy package files
COPY package.json package-lock.json* ./
# Install production dependencies only
RUN npm ci --omit=dev && \
npm cache clean --force
# Copy built application from builder stage
COPY --from=builder /app/dist ./dist
# Set ownership to non-root user
RUN chown -R nodejs:nodejs /app
USER nodejs
EXPOSE 3000
CMD ["node", "dist/index.js"]Comparison with Other Tools
| Tool | Analysis | Auto-Fix | Cost Est. | npm Native | Maintained | |------|----------|----------|-----------|------------|------------| | dockerfile-slim | ✅ | ✅ | ✅ | ✅ | ✅ | | Hadolint | ✅ | ❌ | ❌ | ❌ (Haskell) | ✅ | | Dive | ✅ | ❌ | ❌ | ❌ (Go) | ✅ | | SlimToolkit | ✅ | ✅* | ❌ | ❌ (Go) | ✅ | | dockerfile_lint | ✅ | ❌ | ❌ | ✅ | ❌ (2019) | | Dockershrink | ✅ | ✅ | ❌ | ✅ | ⚠️ (Beta) |
*SlimToolkit optimization can break apps with dynamic dependencies
Why dockerfile-slim?
- npm native - No binary installation, just
npx - Instant analysis - No Docker build required
- Actionable suggestions - Not just problems, but specific fixes
- Cost estimation - Justify optimization work to stakeholders
- Actively maintained - Unlike abandoned npm alternatives
CLI Options
Usage: dockerfile-slim [path] [options]
Arguments:
path Path to Dockerfile or directory (default: ".")
Options:
--json Output results as JSON
--fix Generate an optimized Dockerfile
-o, --output <file> Output file for generated Dockerfile
--dockerignore Generate a .dockerignore file
-V, --version Output version number
-h, --help Display helpCI/CD Integration
# GitHub Actions example
- name: Analyze Dockerfile
run: npx dockerfile-slim --json > dockerfile-analysis.json
- name: Check for critical issues
run: |
CRITICAL=$(cat dockerfile-analysis.json | jq '.optimizations | map(select(.severity == "critical")) | length')
if [ "$CRITICAL" -gt 0 ]; then
echo "Found $CRITICAL critical Dockerfile issues"
exit 1
fiOptimization Database
Includes rules for:
Base Images:
- Ubuntu → Debian Slim → Alpine
- Node.js → node:slim → node:alpine → distroless
- Python → python:slim → python:alpine → distroless
- Go → golang:alpine → scratch/distroless
- Java → eclipse-temurin:jre → distroless
Common Issues:
- Missing multi-stage builds
- apt-get without cleanup
- npm/pip cache not cleaned
- Multiple RUN commands (layer bloat)
- Poor layer caching order
- Running as root
- Using :latest tag
Requirements
- Node.js 18.0.0 or higher
Contributing
Contributions welcome! Especially:
- Adding more optimization patterns
- Improving size estimates
- Adding more base image alternatives
- Better language-specific Dockerfile generation
Related Projects
- Hadolint - Dockerfile linter (Haskell)
- Dive - Image layer explorer (Go)
- SlimToolkit - Container optimizer (Go)
Support
This project is maintained in my free time. If it helped reduce your Docker image size or saved you debugging time, I'd really appreciate your support:
- ⭐ Star the repo—it helps others discover this tool
- 📢 Share with your team or on social media
- 🐛 Report bugs or suggest features
- ☕ Buy me a coffee if you'd like to support development
Thank you to everyone who has contributed, shared feedback, or helped spread the word!
License
MIT
Made with ❤️ for smaller Docker images
