Skip to content

maxlar01/docker-image-optimizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐳 Docker Image Optimizer (DIO)

Lint Β· Scan Β· Optimize Β· Enforce β€” for Docker images

Installation β€’ Quick Start β€’ Commands β€’ Pipeline β€’ Policy β€’ CI Integration


Built an automated Docker Image Optimization pipeline that reduced image sizes by up to 85%, eliminated critical CVEs, and enforced security best practices using policy-as-code in CI/CD pipelines.

DIO is an automated pipeline that analyzes Docker images, suggests optimizations, reduces image sizes, and enforces security best practices. Think of it as lint + security scan + optimizer + policy enforcer for Docker images.

✨ Features

Component Description
πŸ” Dockerfile Analyzer Static analysis with 12+ built-in rules (+ Hadolint if installed) detecting anti-patterns and inefficiencies
⚑ Optimizer Engine 7 optimization strategies including base image switching, multi-stage builds, layer combining
πŸ”’ Security Scanner Trivy/Grype integration for CVE detection
πŸ“‹ Policy Enforcer YAML-defined rules for image size, CVE limits, non-root requirements
πŸ“Š Reporter Markdown + JSON reports, PR comment integration
πŸš€ CI Pipeline GitHub Actions workflow with automated analysis on every PR

Installation

From source

git clone https://github.com/maxlar/docker-image-optimizer.git
cd docker-image-optimizer
make build

The binary will be at bin/dio.

Go install

go install github.com/maxlar/docker-image-optimizer/cmd/dio@latest

Quick Start

# Analyze a Dockerfile for issues
dio analyze Dockerfile

# Suggest optimizations
dio optimize Dockerfile

# Auto-fix optimizations and write Dockerfile.optimized
dio optimize Dockerfile --mode autofix

# Run the full pipeline
dio run Dockerfile --skip-scan --skip-build

# Check against policy
dio policy Dockerfile --policy policies/default.yaml

Commands

dio analyze

Static analysis of a Dockerfile. Checks for:

  • ❌ Unpinned base image tags (:latest)
  • ❌ Missing .dockerignore
  • ❌ Too many layers
  • ❌ apt-get without --no-install-recommends
  • ❌ Package cache not cleaned
  • ❌ Running as root
  • ❌ Copying entire build context (COPY . .)
  • ❌ Missing multi-stage build
  • ❌ Unpinned package versions
  • ❌ Consecutive RUN commands
  • ❌ No WORKDIR set
  • ❌ No HEALTHCHECK defined
dio analyze Dockerfile
dio analyze Dockerfile --format json

Hadolint will be used in addition to the static analysis if it is installed and located in PATH.

dio optimize

Analyzes and optimizes Dockerfiles using 7 strategies:

Strategy Description Impact
Base Image Switch to alpine/slim/distroless variants 50-80% size reduction
Combine Layers Merge consecutive RUN commands 10-20% reduction
Multi-Stage Build Separate build and runtime stages 40-70% reduction
Cache Optimization Reorder COPY for better cache hits Faster rebuilds
Non-Root User Add USER instruction Security improvement
Cleanup Clean package manager caches 10-30% reduction
WORKDIR Set proper working directory Best practice

Modes:

  • suggest (default) β€” shows recommendations only
  • autofix β€” applies changes and writes Dockerfile.optimized
dio optimize Dockerfile --mode suggest
dio optimize Dockerfile --mode autofix --output Dockerfile.prod

dio scan

Security vulnerability scanning (requires Trivy or Grype):

dio scan myapp:latest
dio scan myapp:latest --scanner trivy

dio policy

Enforce policy rules against a Dockerfile:

dio policy Dockerfile
dio policy Dockerfile --policy my-policy.yaml

dio run

Full pipeline β€” analyze β†’ optimize β†’ build β†’ scan β†’ policy β†’ report:

dio run Dockerfile
dio run Dockerfile --mode autofix --policy policies/default.yaml
dio run Dockerfile --skip-scan --skip-build --output reports

Pipeline

Git Repo
  β”‚
  β–Ό
CI Pipeline (GitHub Actions)
  β”‚
  β”œβ”€β”€β–Ά Dockerfile Analyzer (static analysis)
  β”‚
  β”œβ”€β”€β–Ά Image Build (baseline metrics)
  β”‚
  β”œβ”€β”€β–Ά Security Scanner (Trivy/Grype)
  β”‚
  β”œβ”€β”€β–Ά Optimizer Engine (7 strategies)
  β”‚
  β”œβ”€β”€β–Ά Rebuild Optimized Image
  β”‚
  β”œβ”€β”€β–Ά Policy Gate (pass/fail)
  β”‚
  β–Ό
Report + Artifacts (Markdown/JSON)

Policy

Define rules in YAML:

# policies/default.yaml
max_image_size: "500MB"
forbid_latest_tag: true
require_non_root: true
max_critical_cves: 0
max_high_cves: 5
max_layers: 20
min_score: 50

The pipeline fails if any rule is violated β€” perfect for CI gate enforcement.

CI Integration

DIO ships with a GitHub Actions workflow (.github/workflows/dio.yml) that:

  1. Builds and tests DIO
  2. Analyzes your Dockerfile
  3. Runs the optimization pipeline
  4. Posts a report as a PR comment
  5. Fails the pipeline on policy violations

Example PR Comment

βœ… Image optimized successfully

Size reduced: 1.2GB β†’ 180MB (-85%)
Critical CVEs: 42 β†’ 0
Recommendations applied:
βœ” Multi-stage build
βœ” Distroless base
βœ” Non-root user

Project Structure

docker-image-optimizer/
β”œβ”€β”€ cmd/dio/              # CLI entrypoint
β”‚   └── main.go
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ analyzer/         # Dockerfile static analysis + rules
β”‚   β”œβ”€β”€ builder/          # Docker build + metrics collection
β”‚   β”œβ”€β”€ scanner/          # Trivy/Grype security scanning
β”‚   β”œβ”€β”€ optimizer/        # Core optimization engine + strategies
β”‚   β”œβ”€β”€ policy/           # Policy enforcement (YAML rules)
β”‚   β”œβ”€β”€ reporter/         # Markdown + JSON report generation
β”‚   └── models/           # Shared types
β”œβ”€β”€ pkg/docker/           # Docker CLI wrapper
β”œβ”€β”€ policies/             # Default policy config
β”œβ”€β”€ testdata/             # Sample Dockerfiles
β”œβ”€β”€ .github/workflows/    # CI pipeline
β”œβ”€β”€ Makefile              # Build automation
└── go.mod

Tech Stack

Area Technology
Language Go
CLI Cobra
CI GitHub Actions
Image Build Docker / BuildKit
Analysis Custom rules engine + Hadolint
Security Trivy / Grype
Policy YAML-based rules engine
Output Markdown + JSON

Development

# Build
make build

# Run tests
make test

# Run with sample Dockerfile
make run-analyze
make run-optimize
make run-pipeline

# Cross-compile for all platforms
make build-all

Author

Moustafa Rakha (Maxlar)

License

MIT License β€” see LICENSE for details.

About

Built an automated Docker Image Optimization pipeline that reduced image sizes by up to 85%, eliminated critical CVEs, and enforced security best practices using policy-as-code in CI/CD pipelines.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors