AWS Builder Center
Building Dockerfile Doctor: An AI-powered Dockerfile Optimizer with Kiro and Streamlit

Building Dockerfile Doctor: An AI-powered Dockerfile Optimizer with Kiro and Streamlit

An AI-powered tool that automatically rewrites Dockerfiles using industry best practices — reducing image size, improving security, and generating a clean, production-ready Dockerfile in seconds.

Introduction

Dockerfiles are everywhere — from microservices to ML pipelines to serverless workloads masquerading as containers. But most Dockerfiles in the wild? Let’s just say they're… not healthy.
Layers duplicated. Security hardening missing. RUN commands exploding cache. No non-root users. No structure. No reproducibility. No love.
So I built Dockerfile Doctor — an AI-powered tool that automatically rewrites Dockerfiles using industry best practices, helping developers ship smaller, safer, and production-ready containers in seconds.
This project is built using Kiro + Streamlit, with a modular rules engine powering the optimization.



Why I Built This

Every week I work with DevOps, MLOps, and GenAI systems. No matter how advanced the stack is, I see the same problems:
  • Images are too big
  • No build caching
  • No non-root user
  • Excessive RUN layers
  • Missing security hardening
  • ML images bloated with unnecessary dependencies
I wanted a tool that:
✔ Reads your Dockerfile ✔ Understands it line by line ✔ Identifies anti-patterns ✔ Rewrites the entire file ✔ Gives you a diff ✔ Scores it with a Zero-Trust model
…in one click.



How I Built It (Architecture)

dockerfile-doctor/
├── dockerdoctor/
│ ├── analyzer.py # Tokenizer + line parser
│ ├── optimizer.py # Best-practice rules engine
│ ├── security.py # Zero-Trust scoring system
│ ├── mlops_mode.py # MLOps stack detector (Torch, TF, HF, vLLM)
│ ├── diff_utils.py # Unified diff generator
│ └── models.py # Pydantic models
├── frontend/
│ └── app.py # Streamlit UI
└── tests/


The Engine Behind It

The rules engine performs:
  • RUN layer flattening
  • Non-root user injection
  • Security linting
  • Base-image recommendations
  • MLOps stack auto-detection
  • Cache optimization
  • Instruction normalization
It returns:
  • Optimized Dockerfile content
  • A Zero-Trust security score
  • Applied rules list
  • A unified diff
The UI renders everything cleanly using Streamlit.



Features:

AI-Powered Optimization

  • Rewrite RUN commands
  • Fix broken patterns
  • Remove duplication
  • Reorder instructions
  • Convert CMD to JSON array format
  • Add non-root user automatically

Zero-Trust Security Score

A 0–100 score based on:
  • Root vs non-root
  • Unsafe instructions
  • Shell vs exec form
  • Image provenance
  • Secret exposure patterns

MLOps Smart Mode

Detects frameworks like:
  • PyTorch
  • TensorFlow
  • Transformers
  • HuggingFace tooling
  • CUDA dependencies
  • vLLM / FastAPI ML workloads
…and applies ML-specific optimizations.
What I Learned While Building This
  • How to design a modular rules engine
  • How to build a UI/UX that feels instant and developer-friendly
  • How to integrate security checks into DevOps tooling
  • How to score container hardening effectively
  • How to use Kiro for fast backend iteration
This project gave me clarity on how small rewrites drastically improve Dockerfile hygiene.



Roadmap

Coming soon:
  • CLI tool:  dockerfile-doctor --fix Dockerfile 
  • GitHub Action: Auto-PR with optimized Dockerfile
  • BuildKit caching heatmap
  • CVE scanning via Trivy
  • VSCode extension



🔗 Source Code

(Repo will be made public after publishing this article.)



Final Thoughts

Dockerfile Doctor started as a weekend experiment… and quickly became a tool I now use every single day in my DevOps and MLOps workflows.
If this helps even one developer ship cleaner images, the project was worth it. If it helps thousands — that’s the dream.
Let's build clean. Let's build secure. Let's build fast.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article