Skip to content

danieleschmidt/docker-optimizer-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Docker Optimizer Agent

CI Coverage Security PyPI version Python 3.9+

Intelligent Dockerfile optimization with security scanning, performance analysis, and best practices enforcement.

Docker Optimizer Agent analyzes Dockerfiles and automatically generates optimized, secure, and efficient container configurations. It combines static analysis, security scanning, and performance optimization to produce production-ready Dockerfiles following industry best practices.

πŸš€ Key Features

  • πŸ”’ Security-First Optimization: Vulnerability scanning with Trivy, security scoring (A-F grades), and automated security fixes
  • πŸ“ Intelligent Size Reduction: Multi-stage builds, minimal base images, layer optimization (up to 80% size reduction)
  • ⚑ Performance Enhancement: Parallel processing, intelligent caching, build context optimization
  • πŸ›‘οΈ Best Practices Enforcement: Non-root users, specific versions, health checks, proper signal handling
  • πŸ”„ Batch Processing: Optimize multiple Dockerfiles simultaneously with performance monitoring
  • πŸ“Š Comprehensive Reporting: Detailed explanations, metrics, and security analysis in JSON/YAML/text formats

πŸ“¦ Installation

# Install from PyPI
pip install docker-optimizer-agent

# Install with security scanning support
pip install docker-optimizer-agent[security]

# Install development dependencies
pip install docker-optimizer-agent[dev]

πŸƒ Quick Start

Command Line Interface

# Basic optimization
docker-optimizer --dockerfile Dockerfile --output Dockerfile.optimized

# Security scanning with optimization
docker-optimizer --dockerfile Dockerfile --security-scan --format json

# Multi-stage build generation  
docker-optimizer --dockerfile Dockerfile --multistage

# Performance optimization with reporting
docker-optimizer --dockerfile Dockerfile --performance --performance-report

# Batch processing multiple Dockerfiles
docker-optimizer --batch service1/Dockerfile --batch service2/Dockerfile --performance

Python API

from docker_optimizer import DockerfileOptimizer
from docker_optimizer.external_security import ExternalSecurityScanner

# Basic optimization
optimizer = DockerfileOptimizer()
with open('Dockerfile', 'r') as f:
    result = optimizer.optimize_dockerfile(f.read())

print(f"Size reduction: {result.original_size} β†’ {result.optimized_size}")
print(f"Explanation: {result.explanation}")

# Security scanning
scanner = ExternalSecurityScanner()
vuln_report = scanner.scan_dockerfile_for_vulnerabilities(dockerfile_content)
security_score = scanner.calculate_security_score(vuln_report)
print(f"Security grade: {security_score.grade}")

# Multi-stage optimization  
from docker_optimizer.multistage import MultiStageOptimizer
multistage = MultiStageOptimizer()
ms_result = multistage.generate_multistage_dockerfile(dockerfile_content)
print(ms_result.optimized_dockerfile)

πŸ“‹ Documentation

🎯 Optimization Categories

πŸ”’ Security Enhancements

  • Vulnerability Scanning: Trivy integration with CVE database lookup
  • User Security: Non-root user creation and privilege management
  • Version Pinning: Specific package versions to prevent supply chain attacks
  • Base Image Security: Recommendations for minimal, secure base images

πŸ“ Size Reduction (up to 80%)

  • Multi-Stage Builds: Separate build and runtime environments
  • Minimal Base Images: Alpine, distroless, and slim variants
  • Layer Optimization: Combined commands and cache cleanup
  • Build Context: .dockerignore optimization

⚑ Performance Optimization

  • Parallel Processing: Multi-threaded Dockerfile analysis
  • Intelligent Caching: LRU cache with TTL for optimization results
  • Layer Caching: Optimal instruction ordering for Docker layer cache
  • Batch Processing: Process multiple Dockerfiles simultaneously

πŸ›‘οΈ Best Practices Enforcement

  • Health Checks: Automatic health check generation
  • Signal Handling: Proper SIGTERM handling with STOPSIGNAL
  • Metadata Labels: Monitoring, security, and compliance labels
  • Resource Management: Memory limits and CPU constraints

πŸ“Š Example Optimization

Before (Vulnerable, 180MB):

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y python3 python3-pip curl
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY . /app
WORKDIR /app
CMD ["python3", "app.py"]

After (Secure, 65MB, A+ Security Grade):

# Multi-stage build for optimal size and security
FROM ubuntu:22.04-slim AS builder
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3=3.10.6-1~22.04 \
    python3-pip=22.0.2+dfsg-1 && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip3 install --user --no-cache-dir -r requirements.txt

FROM ubuntu:22.04-slim AS runtime
RUN groupadd -r appuser && useradd -r -g appuser appuser

# Copy Python packages from builder
COPY --from=builder /root/.local /home/appuser/.local
COPY --chown=appuser:appuser . /app

USER appuser
WORKDIR /app
ENV PATH=/home/appuser/.local/bin:$PATH

HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
    CMD curl -f http://localhost:8000/health || exit 1

CMD ["python3", "app.py"]

Improvements:

  • βœ… 65% size reduction (180MB β†’ 65MB)
  • βœ… Security grade A+ (was F)
  • βœ… Multi-stage build separating build and runtime
  • βœ… Non-root user for security
  • βœ… Specific versions preventing vulnerabilities
  • βœ… Health check for monitoring
  • βœ… Optimized layers for better caching

πŸ”§ CLI Reference

docker-optimizer [OPTIONS]

Options:
  -f, --dockerfile PATH    Path to Dockerfile (default: ./Dockerfile)
  -o, --output PATH        Output path for optimized Dockerfile
  --analysis-only          Only analyze without optimizing
  --format [text|json|yaml] Output format (default: text)
  -v, --verbose           Enable verbose output
  --multistage            Generate multi-stage build optimization
  --security-scan         Perform external security vulnerability scan
  --performance           Enable performance optimizations (caching, parallel)
  --batch PATH            Process multiple Dockerfiles (repeatable)
  --performance-report    Show performance metrics after optimization
  --help                  Show this message and exit

πŸ”— Integration Examples

GitHub Actions CI/CD

- name: Optimize Dockerfiles
  run: |
    docker-optimizer --dockerfile Dockerfile --security-scan --format json
    if [ "$(jq -r '.security_score.grade' report.json)" = "F" ]; then
      echo "❌ Security grade F - build failed"
      exit 1
    fi

Pre-commit Hook

- repo: local
  hooks:
    - id: dockerfile-optimization
      name: Dockerfile Security & Optimization
      entry: docker-optimizer --security-scan
      language: system
      files: Dockerfile.*

Docker Compose Integration

# Optimize all service Dockerfiles
find . -name "Dockerfile*" -path "*/services/*" | \
  xargs -I {} docker-optimizer --dockerfile {} --multistage --performance

πŸ“ˆ Performance Metrics

  • 87.45% Test Coverage - Comprehensive test suite
  • 115 Passing Tests - Robust validation across all modules
  • Sub-second Optimization - Fast analysis and optimization
  • Parallel Processing - Multi-threaded batch processing
  • Intelligent Caching - LRU cache with TTL reduces duplicate work

πŸ› οΈ Development

# Clone the repository
git clone https://github.com/danieleschmidt/docker-optimizer-agent.git
cd docker-optimizer-agent

# Install development dependencies
pip install -e ".[dev,security]"

# Run tests
pytest tests/ --cov=docker_optimizer --cov-fail-under=85

# Run linting
ruff check src/ tests/
black --check src/ tests/
mypy src/

# Run security scan
bandit -r src/

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Run tests and linting (pytest && ruff check)
  4. Commit your changes (git commit -m 'Add amazing feature')
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

πŸ“„ License

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

πŸ™ Acknowledgments

  • Trivy for vulnerability scanning
  • Docker for containerization platform
  • Click for CLI framework
  • Pydantic for data validation

Made with ❀️ by the Docker Optimizer Agent team

Report Issues β€’ Request Features β€’ Security Issues

About

Suggests minimal secure Dockerfiles

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Contributors 5

Languages