A Two-Stage LLM Pipeline for generating optimized Dockerfiles
Project description
DockAI
The End of Manual Dockerfiles: Automated, Intelligent, Production-Ready.
DockAI is a robust, enterprise-grade Python CLI tool designed to intelligently analyze a software repository and generate a production-ready, optimized Dockerfile. It uses a novel two-stage LLM pipeline to first understand the project structure ("The Brain") and then architect the build environment ("The Architect").
💡 Why DockAI?
Automated Dockerfiles > Human Written > Cloud Native Buildpacks
DockAI represents the next evolution in containerization.
- Better than Humans: Humans forget best practices, security patches, and layer optimizations. DockAI applies the collective knowledge of thousands of expert DevOps engineers to every single build, ensuring multi-stage optimization, non-root users, and perfect caching strategies every time.
- Better than Buildpacks: Cloud Native Buildpacks are opaque "black boxes" that add bloat and are hard to debug. DockAI generates a transparent, standard Dockerfile that you can read, audit, and modify. You get the automation of buildpacks with the control of a handwritten file.
✨ Key Features
- Zero-Config Automation: Developers never need to write a Dockerfile again. The GitHub Action automatically generates a perfect, up-to-date Dockerfile on every commit.
- Two-Stage Pipeline: Separates analysis (cheap/fast) from generation (smart/expensive) for cost-efficiency.
- Intelligent Scanning: Uses
pathspecto fully respect.gitignoreand.dockerignorepatterns (including wildcards like*.logorsecret_*.json). - Robust & Reliable: Built-in automatic retries with exponential backoff for all AI API calls to handle network instability.
- Observability: Structured logging with a
--verbosemode for deep debugging and transparency. - Security First: Generates non-root, multi-stage builds by default.
🧠 Architecture
The system operates in three distinct phases:
-
The Intelligent Scanner (
scanner.py):- Maps the entire repository file tree.
- Automatically filters out files based on
.gitignoreand.dockerignoreusing industry-standard wildcard matching.
-
Stage 1: The Brain (
analyzer.py):- Input: JSON list of file paths.
- Task: Identifies the technology stack (e.g., Python/Flask, Node/Express) and pinpoints the exact files needed for context (e.g.,
package.json,requirements.txt).
-
Stage 2: The Architect (
generator.py):- Input: Content of the critical files identified in Stage 1.
- Task: Writes a multi-stage, security-focused Dockerfile with version pinning and cache optimization.
🚀 Getting Started
Prerequisites
- Python 3.8+
- An OpenAI API Key
Installation
From PyPI (Recommended):
pip install dockai-cli
From Source (Development):
-
Clone the repository:
git clone https://github.com/itzzjb/dockai.git cd dockai
-
Install the package: You can install the tool locally using pip. We recommend installing in "editable" mode (
-e) if you plan to modify the code.pip install -e .
-
Configure Environment: Create a
.envfile in the root directory and add your OpenAI API key and model configurations:OPENAI_API_KEY=sk-your-api-key-here MODEL_ANALYZER=gpt-4o-mini MODEL_GENERATOR=gpt-4o
🤖 Usage as GitHub Action
You can use DockAI directly in your GitHub Actions workflow to automatically generate a Dockerfile on every push. This ensures your Dockerfile is always perfectly in sync with your code changes, without any manual intervention.
Example Workflow
Create a file .github/workflows/dockai.yml:
name: Generate Dockerfile
on:
workflow_dispatch: # Allows manual triggering
jobs:
generate:
runs-on: ubuntu-latest
permissions:
contents: write # Needed to push the generated Dockerfile back
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run DockAI
uses: itzzjb/dockai-cli@main
with:
openai_api_key: ${{ secrets.OPENAI_API_KEY }}
model_analyzer: gpt-4o-mini
model_generator: gpt-4o
- name: Commit and Push Dockerfile
run: |
git config --global user.name "DockAI Bot"
git config --global user.email "bot@dockai-cli.com"
git add Dockerfile
git commit -m "ci: generate optimized Dockerfile via DockAI" || echo "No changes to commit"
git push
💻 CLI Usage
Once installed, the dockai-cli command is available globally in your terminal.
Run the tool by pointing it to the target repository path.
dockai-cli /path/to/target/repo
Example (Current Directory):
dockai-cli .
Verbose Mode (for debugging):
dockai-cli . --verbose
What to Expect
The CLI uses a rich terminal interface to show progress:
- Scanning: Locates files, respecting all ignore patterns.
- Analyzing: "The Brain" decides what matters.
- Reading: Only reads the content of critical files (privacy/token efficient).
- Generating: "The Architect" builds the Dockerfile.
- Result: A
Dockerfileis saved to the target directory.
🎨 Custom Instructions
DockAI supports custom instructions to tailor the Dockerfile generation to your specific needs. You can provide instructions in natural language using two methods:
Method 1: Environment Variables
Set environment variables to provide instructions:
export DOCKAI_ANALYZER_INSTRUCTIONS="Always include package-lock.json if it exists"
export DOCKAI_GENERATOR_INSTRUCTIONS="Use port 8080 and install ffmpeg"
dockai-cli .
Or in your .env file:
DOCKAI_ANALYZER_INSTRUCTIONS="Always include package-lock.json if it exists."
DOCKAI_GENERATOR_INSTRUCTIONS="Ensure all images are based on Alpine Linux."
Method 2: .dockai-cli File
Create a .dockai-cli file in your project root with section-based instructions:
# Instructions for the analyzer (file selection stage)
[analyzer]
Always include package-lock.json or yarn.lock if they exist.
Look for any .env.example files to understand environment variables.
Include docker-compose.yml if present.
# Instructions for the generator (Dockerfile creation stage)
[generator]
Ensure the container runs as a non-root user named 'appuser'.
Do not expose any ports other than 8080.
Install 'curl' and 'vim' for debugging purposes.
Set the timezone to 'UTC'.
Define an environment variable 'APP_ENV' with value 'production'.
Note: If you don't use sections ([analyzer] and [generator]), the instructions will be applied to both stages.
Use Cases for Custom Instructions
Analyzer Instructions:
- "Always include lock files (package-lock.json, yarn.lock, poetry.lock)"
- "Look for configuration files in the config/ directory"
- "Include any .proto files for gRPC services"
Generator Instructions:
- "Use Alpine-based images only"
- "Install system dependencies: ffmpeg, imagemagick, ghostscript"
- "Expose port 3000 instead of the default"
- "Add health check using curl to /health endpoint"
- "Set NODE_ENV to production"
- "Create a non-root user named 'nodeuser'"
GitHub Action with Custom Instructions
- name: Run DockAI
uses: itzzjb/dockai@main
with:
openai_api_key: ${{ secrets.OPENAI_API_KEY }}
model_analyzer: gpt-4o-mini
model_generator: gpt-4o
analyzer_instructions: "Always include yarn.lock if present"
generator_instructions: "Use Alpine Linux and install curl"
🛠️ Development
Running Tests
This project uses pytest for testing. To run the test suite:
pytest
Project Structure
The project follows a modern src-layout:
src/dockai/: Source code package.main.py: The CLI orchestrator usingtyperandrich.scanner.py: Directory traversal logic withpathspec.analyzer.py: Interface for the Stage 1 LLM call (with retries).generator.py: Interface for the Stage 2 LLM call (with retries).
tests/: Unit and integration tests.pyproject.toml: Build configuration and dependency management.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dockai_cli-0.1.3.tar.gz.
File metadata
- Download URL: dockai_cli-0.1.3.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0c8232871032f11bf3e4e492702b8c932ee2675f699d6ba2df3ce592cff7d28
|
|
| MD5 |
cea2c53dfec71680ce89e1e5d85aaea2
|
|
| BLAKE2b-256 |
d02188a22363ce9346423885e75fccb7a5733d7f65db397e127e8fedebd79731
|
File details
Details for the file dockai_cli-0.1.3-py3-none-any.whl.
File metadata
- Download URL: dockai_cli-0.1.3-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff0352eae47dddd53610a0d1464f23dac0dac8ce575a9fce6ea5a80dd6f36b2f
|
|
| MD5 |
5ffff870fda18e1411d3e99912791b4e
|
|
| BLAKE2b-256 |
3e401453d74768458ebb7089e178beed08eed6551ec05ec4ff98a58970a9e66e
|