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.
| 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 |
git clone https://github.com/maxlar/docker-image-optimizer.git
cd docker-image-optimizer
make buildThe binary will be at bin/dio.
go install github.com/maxlar/docker-image-optimizer/cmd/dio@latest# 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.yamlStatic analysis of a Dockerfile. Checks for:
- β Unpinned base image tags (
:latest) - β Missing
.dockerignore - β Too many layers
- β
apt-getwithout--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 jsonHadolint will be used in addition to the static analysis if it is installed and located in PATH.
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 onlyautofixβ applies changes and writesDockerfile.optimized
dio optimize Dockerfile --mode suggest
dio optimize Dockerfile --mode autofix --output Dockerfile.prodSecurity vulnerability scanning (requires Trivy or Grype):
dio scan myapp:latest
dio scan myapp:latest --scanner trivyEnforce policy rules against a Dockerfile:
dio policy Dockerfile
dio policy Dockerfile --policy my-policy.yamlFull 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 reportsGit 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)
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: 50The pipeline fails if any rule is violated β perfect for CI gate enforcement.
DIO ships with a GitHub Actions workflow (.github/workflows/dio.yml) that:
- Builds and tests DIO
- Analyzes your Dockerfile
- Runs the optimization pipeline
- Posts a report as a PR comment
- Fails the pipeline on policy violations
β
Image optimized successfully
Size reduced: 1.2GB β 180MB (-85%)
Critical CVEs: 42 β 0
Recommendations applied:
β Multi-stage build
β Distroless base
β Non-root user
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
| 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 |
# 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-allMoustafa Rakha (Maxlar)
MIT License β see LICENSE for details.