Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐳 DockerOpt - Dockerfile Optimizer

Free and open-source Dockerfile optimizer that helps you build smaller, faster, and more secure Docker images.

License: MIT GitHub stars npm version

✨ Features

  • πŸ” Intelligent Analysis - AI-powered analysis with Gemini for detailed Dockerfile optimization
  • πŸ“¦ Size Reduction - Reduce image size by up to 70% with optimized base images and multi-stage builds
  • πŸ”’ Security Fixes - Automatically detect and fix security issues (non-root user, pinned versions, etc.)
  • ⚑ Performance - Optimize layer caching and reduce build time by up to 42%
  • πŸ“Š Detailed Reports - Get comprehensive reports with before/after comparisons
  • 🌐 Web Interface - Beautiful web UI for instant Dockerfile optimization
  • πŸ› οΈ CLI Tool - Command-line tool for CI/CD integration
  • πŸ“ Multiple Formats - Support for text, JSON, Markdown, and SARIF output formats
  • 🧬 Image Inspector - Deep dive into Docker image layers (like Dive)
  • πŸ”„ Build Replay - Visual timeline of Docker build process with cache analysis
  • 🧠 Layer AI - AI-powered layer behavior analysis and optimization suggestions
  • βš™οΈ Config Wizard - Interactive wizard for Docker configuration optimization
  • πŸ”Ž Secret Detection - Automatically detect secrets and sensitive data
  • πŸ“ˆ Layer DNA Compare - Visual comparison of image layer composition

πŸš€ Quick Start

Web Interface (Recommended)

Visit the web interface for instant optimization:

  1. Paste your Dockerfile
  2. Click "Optimize"
  3. Get optimized Dockerfile with detailed analysis

CLI Installation

Option 1: Build from Source

git clone https://github.com/duongtech/dockeropt.git
cd dockeropt
npm install
npm run build
npm link

Option 2: Use with npx (Temporary)

npx -y dockeropt lint Dockerfile
npx -y dockeropt fix Dockerfile

Option 3: Global Install (After Publishing to npm)

npm install -g dockeropt

πŸ“– Usage

Basic Commands

# Lint Dockerfile for issues
dockeropt lint Dockerfile

# Optimize and generate fixed Dockerfile
dockeropt fix Dockerfile -o out

# Run in CI mode (fails on high severity issues)
dockeropt ci Dockerfile --fail-on high

# Explain Dockerfile structure and suggestions
dockeropt explain Dockerfile

CLI Options

lint Command

dockeropt lint <dockerfile> [options]

Options:
  -f, --format <format>     Output format: text, json, sarif (default: text)
  --context <path>          Path to build context directory
  --package-manager <pm>    Package manager: npm, pnpm, yarn, apt, apk, pip, poetry, go, cargo

fix Command

dockeropt fix <dockerfile> [options]

Options:
  -o, --output <path>       Output directory (default: out)
  -f, --format <format>    Report format: text, markdown, json (default: markdown)
  --context <path>          Path to build context directory
  --package-manager <pm>   Package manager type

ci Command

dockeropt ci <dockerfile> [options]

Options:
  --format <format>         Output format: sarif, json (default: sarif)
  --out <file>              Output file path (default: results.sarif)
  --fail-on <severity>     Fail on severity: high, medium, low (default: high)
  --context <path>          Path to build context directory

πŸ“ Examples

Example: Before and After

Before (Unoptimized):

FROM node:18
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
RUN apt-get update
RUN apt-get install -y curl wget
EXPOSE 3000
CMD ["npm", "start"]

After (Optimized):

FROM node:18-alpine@sha256:a6385524b09b9de27e332b22e90fb7a70e3adf1a41a54edd0c8e6e597f4e9aaf AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY . .
RUN npm run build

FROM node:18-alpine@sha256:a6385524b09b9de27e332b22e90fb7a70e3adf1a41a54edd0c8e6e597f4e9aaf
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER node
CMD ["node", "dist/index.js"]

Results:

  • Size reduction: ~135 MB
  • Layer reduction: 3 layers
  • Build time improvement: ~42%
  • Security: Non-root user added
  • Cache efficiency: Improved

πŸ”§ Optimization Rules

DockerOpt applies 11+ optimization rules:

  • βœ… Multi-stage builds - Separate build and runtime stages
  • βœ… Layer caching - Optimize COPY order for better cache hits
  • βœ… Base image optimization - Use Alpine, slim, or distroless variants
  • βœ… Pin digest - Pin base images with SHA256 for reproducible builds
  • βœ… Non-root user - Run containers as non-root for security
  • βœ… Clean package managers - Remove package manager cache
  • βœ… Combine RUN commands - Reduce number of layers
  • βœ… Use .dockerignore - Exclude unnecessary files
  • βœ… Pin versions - Use specific versions instead of latest
  • βœ… Minimize layers - Combine related operations
  • βœ… Security scanning - Detect security vulnerabilities

🎯 Supported Languages

DockerOpt has optimized examples for:

  • Node.js - npm, yarn, pnpm
  • Python - pip, poetry
  • Go - Go modules
  • Java - Maven, Gradle
  • Rust - Cargo
  • PHP - Composer
  • Ruby - Bundler
  • And more...

🌐 Web Interface

The web interface provides:

  • πŸ“ Live Editor - Monaco editor with syntax highlighting
  • πŸ“Š Visual Metrics - Size, layer, and build time comparisons
  • πŸ” Side-by-Side Diff - Before/After comparison
  • 🎨 Beautiful UI - Modern, responsive design
  • πŸ”— GitHub Integration - Fetch Dockerfiles directly from GitHub URLs
  • 🌍 Multi-language Examples - Node, Go, Python, Java examples
  • πŸ“‹ One-Click Copy - Copy optimized Dockerfile instantly
  • 🧬 Image Analysis - Inspect and analyze existing Docker images
  • πŸ”„ Build Timeline - Visualize build process with cache efficiency
  • 🧠 AI Suggestions - Get intelligent layer optimization recommendations
  • βš™οΈ Interactive Wizard - Step-by-step Docker configuration guide

Setup Web Interface

cd web
npm install
npm run dev  # Development server on http://localhost:3000
npm run build  # Production build

Note: For AI-powered features, set VITE_GEMINI_API_KEY in .env file:

VITE_GEMINI_API_KEY=your_api_key_here

Get your API key from Google AI Studio.

πŸ”’ Security & Privacy

  • 100% Local - All analysis runs locally in your browser/CLI
  • No Data Collection - Your Dockerfile never leaves your machine
  • Open Source - Full source code available for audit
  • No Backend - No server, no database, no logging

πŸ› οΈ Development

Prerequisites

  • Node.js 18+
  • npm or yarn

Setup

# Clone repository
git clone https://github.com/DangNgocDuong250903/dockeropt.git
cd dockeropt

# Install dependencies
npm install

# Build TypeScript
npm run build

# Run tests
npm test

# Lint code
npm run lint

Project Structure

dockeropt/
β”œβ”€β”€ src/                 # Core optimization engine (TypeScript)
β”‚   β”œβ”€β”€ cli.ts          # CLI entry point
β”‚   β”œβ”€β”€ optimizer.ts    # Main optimizer
β”‚   β”œβ”€β”€ rules.ts        # Optimization rules
β”‚   β”œβ”€β”€ parser.ts        # Dockerfile parser
β”‚   └── ...
β”œβ”€β”€ web/                # Web interface
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ app.js      # Main app logic
β”‚   β”‚   β”œβ”€β”€ components/ # UI components
β”‚   β”‚   └── ...
β”‚   └── ...
β”œβ”€β”€ examples/           # Example Dockerfiles
└── dist/               # Compiled output

πŸ“Š Output Formats

Text Format

πŸ“‹ Dockerfile Lint Results

HIGH (2):
  1. Base image should use specific tag instead of 'latest'
     Fix: Use 'node:18-alpine@sha256:...'
  2. Container runs as root user
     Fix: Add 'USER node' or create non-root user

JSON Format

{
  "findings": [
    {
      "severity": "high",
      "message": "Base image should use specific tag",
      "lineNumber": 1,
      "suggestion": "Use 'node:18-alpine@sha256:...'"
    }
  ],
  "metrics": {
    "estimatedSizeSavings": 135,
    "layerReduction": 3,
    "securityScore": 85
  }
}

SARIF Format

For integration with GitHub Advanced Security, CodeQL, and other security tools.

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ“ž Support

⭐ Star History

If you find DockerOpt useful, please consider giving it a star on GitHub!


Made with ❀️ by Đặng Ngọc DưƑng

About

Optimize Dockerfiles instantly with AI. Shrink images, speed up builds, fix security

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages