Skip to content

Docker Architecture Diagram & Overview#

πŸ—οΈ Multi-Stage Build Architecture#

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Dockerfile                              β”‚
β”‚                    (148 lines, 6 stages)                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚               β”‚               β”‚
              β–Ό               β–Ό               β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Builder  β”‚    β”‚ Runtime  β”‚   β”‚   Dev    β”‚
        β”‚ Stage 1  │───▢│  Base    │◀──│ Stage 6  β”‚
        β”‚          β”‚    β”‚ Stage 2  β”‚   β”‚          β”‚
        β”‚ - uv     β”‚    β”‚          β”‚   β”‚ - pytest β”‚
        β”‚ - build  β”‚    β”‚ - git    β”‚   β”‚ - black  β”‚
        β”‚ - wheel  β”‚    β”‚ - deps   β”‚   β”‚ - flake8 β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚ - mypy   β”‚
                              β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚             β”‚             β”‚
                β–Ό             β–Ό             β–Ό
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚  CLI   β”‚   β”‚  API   β”‚   β”‚  MCP   β”‚
            β”‚ Stage  β”‚   β”‚ Stage  β”‚   β”‚ Stage  β”‚
            β”‚   3    β”‚   β”‚   4    β”‚   β”‚   5    β”‚
            β”‚        β”‚   β”‚        β”‚   β”‚        β”‚
            β”‚ Entry: β”‚   β”‚ Entry: β”‚   β”‚ Entry: β”‚
            β”‚  CLI   β”‚   β”‚Uvicornβ”‚   β”‚ MCP    β”‚
            β”‚ tool   β”‚   β”‚ REST   β”‚   β”‚ Server β”‚
            β”‚        β”‚   β”‚        β”‚   β”‚        β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚             β”‚             β”‚
                β–Ό             β–Ό             β–Ό
        terraform-ingest   terraform-ingest  terraform-ingest
           :cli             :api              :mcp
        (~350MB)         (~350MB)           (~350MB)

πŸ“¦ Image Specification#

Base Layer (All Images)#

Python 3.13-slim
β”œβ”€β”€ apt dependencies
β”‚   β”œβ”€β”€ git
β”‚   └── ca-certificates
β”œβ”€β”€ Python packages (via pip)
β”‚   β”œβ”€β”€ click
β”‚   β”œβ”€β”€ fastapi
β”‚   β”œβ”€β”€ fastmcp
β”‚   β”œβ”€β”€ gitpython
β”‚   β”œβ”€β”€ httpx
β”‚   β”œβ”€β”€ loguru
β”‚   β”œβ”€β”€ pydantic
β”‚   β”œβ”€β”€ python-hcl2
β”‚   β”œβ”€β”€ pyyaml
β”‚   └── uvicorn
└── Application (from wheel)
    └── terraform_ingest

CLI Mode#

runtime-base
β”œβ”€β”€ Entry: terraform-ingest command
β”œβ”€β”€ Features:
β”‚   β”œβ”€β”€ Full Click CLI
β”‚   β”œβ”€β”€ Ingest command
β”‚   β”œβ”€β”€ Analyze command
β”‚   └── Help system
└── Use: Docker run with command args

API Mode#

runtime-base
β”œβ”€β”€ Entry: uvicorn terraform_ingest.api:app
β”œβ”€β”€ Features:
β”‚   β”œβ”€β”€ FastAPI REST service
β”‚   β”œβ”€β”€ Swagger UI (/docs)
β”‚   β”œβ”€β”€ ReDoc (/redoc)
β”‚   β”œβ”€β”€ Health check (/health)
β”‚   β”œβ”€β”€ Ingest endpoint (/ingest)
β”‚   └── Analyze endpoint (/analyze)
β”œβ”€β”€ Port: 8000
└── Use: Long-running service

MCP Mode#

runtime-base
β”œβ”€β”€ Entry: terraform-ingest-mcp
β”œβ”€β”€ Features:
β”‚   β”œβ”€β”€ FastMCP server
β”‚   β”œβ”€β”€ Stdio protocol
β”‚   β”œβ”€β”€ Auto-ingestion
β”‚   β”œβ”€β”€ Periodic refresh
β”‚   β”œβ”€β”€ list_repositories tool
β”‚   └── search_modules tool
└── Use: AI agent integration

Dev Mode#

runtime-base
β”œβ”€β”€ pytest (testing)
β”œβ”€β”€ black (formatting)
β”œβ”€β”€ flake8 (linting)
β”œβ”€β”€ mypy (type checking)
β”œβ”€β”€ ipython (interactive)
β”œβ”€β”€ ipdb (debugging)
└── Entry: /bin/bash (interactive)

πŸš€ Execution Flow by Mode#

CLI Mode Flow#

docker run terraform-ingest:cli ingest config.yaml
         β”‚
         β”œβ”€β–Ί Parse CLI arguments
         β”‚
         β”œβ”€β–Ί Load YAML config
         β”‚
         β”œβ”€β–Ί Ingest repositories
         β”‚   β”œβ”€β–Ί Clone repos
         β”‚   β”œβ”€β–Ί Parse Terraform
         β”‚   └─► Generate JSON
         β”‚
         └─► Exit (0 = success)

API Mode Flow#

docker run -p 8000:8000 terraform-ingest:api
         β”‚
         β”œβ”€β–Ί Start Uvicorn server
         β”‚
         β”œβ”€β–Ί Listen on 0.0.0.0:8000
         β”‚
         β”œβ”€β–Ί Handle requests
         β”‚   β”œβ”€β–Ί GET /health β†’ OK
         β”‚   β”œβ”€β–Ί GET /docs β†’ Swagger UI
         β”‚   β”œβ”€β–Ί POST /ingest β†’ Process
         β”‚   └─► POST /analyze β†’ Process
         β”‚
         └─► Run indefinitely
             (until container stops)

MCP Mode Flow#

docker run terraform-ingest:mcp
         β”‚
         β”œβ”€β–Ί Check config
         β”‚
         β”œβ”€β–Ί Optional: Run ingestion
         β”‚
         β”œβ”€β–Ί Start MCP server
         β”‚
         β”œβ”€β–Ί Listen on stdio
         β”‚
         β”œβ”€β–Ί Handle MCP requests
         β”‚   β”œβ”€β–Ί list_repositories
         β”‚   └─► search_modules
         β”‚
         └─► Run indefinitely
             (until container stops)

πŸ“Š Volume Mount Architecture#

Host System                          Container
─────────────                        ──────────

~/.ssh/
β”œβ”€β”€ id_rsa      ────────────────────► /root/.ssh/id_rsa (ro)
β”œβ”€β”€ id_rsa.pub  ────────────────────► /root/.ssh/id_rsa.pub (ro)
└── known_hosts ────────────────────► /root/.ssh/known_hosts (ro)

$(pwd)/config.yaml ────────────────► /app/config/config.yaml (ro)

$(pwd)/output/
β”œβ”€β”€ module1.json ◄────────────────── /app/output/module1.json
└── module2.json ◄────────────────── /app/output/module2.json

$(pwd)/repos/
β”œβ”€β”€ repo1/ ◄────────────────────── /app/repos/repo1/
└── repo2/ ◄────────────────────── /app/repos/repo2/

.venv/                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ί /app/.venv/ (dev only)

πŸ”„ CI/CD Pipeline Flow#

GitHub Push/Tag/PR
       β”‚
       β–Ό
Docker Build Job (docker-build.yml)
       β”‚
       β”œβ”€β–Ί Setup Docker Buildx
       β”‚
       β”œβ”€β–Ί Extract metadata
       β”‚   β”œβ”€β–Ί Branch tags
       β”‚   β”œβ”€β–Ί Semantic version tags
       β”‚   └─► SHA tags
       β”‚
       β”œβ”€β–Ί Build matrix (4 targets)
       β”‚   β”œβ”€β–Ί cli
       β”‚   β”œβ”€β–Ί api
       β”‚   β”œβ”€β–Ί mcp
       β”‚   └─► dev
       β”‚
       β”œβ”€β–Ί Multi-platform build
       β”‚   β”œβ”€β–Ί linux/amd64
       β”‚   └─► linux/arm64
       β”‚
       β”œβ”€β–Ί Push to GHCR
       β”‚
       β”œβ”€β–Ί Test stage (if PR)
       β”‚   β”œβ”€β–Ί CLI: --help, --version
       β”‚   β”œβ”€β–Ί API: uvicorn check
       β”‚   └─► MCP: startup test
       β”‚
       └─► Security scan (if push)
           β”œβ”€β–Ί Trivy scan
           └─► Upload SARIF

πŸ” Security Model#

Production Image Security
───────────────────────────────

βœ“ Minimal Base Image (python:3.13-slim)
  └─ Only essential packages
     β”œβ”€ git (for cloning)
     └─ ca-certificates (for HTTPS)

βœ“ Multi-Stage Build
  └─ Build tools (uv, gcc) not in final image
     β”œβ”€ Smaller image size
     β”œβ”€ Reduced attack surface
     └─ Faster deployment

βœ“ No Build Tools in Runtime
  └─ gcc, make, etc. excluded
     └─ Prevents runtime compilation

βœ“ Read-Only Config Mounts
  └─ Configuration cannot be modified
     └─ Immutable setup

βœ“ SSH Key Isolation
  └─ Keys in volumes, not in image
     β”œβ”€ No credentials in image
     └─ Easy credential rotation

βœ“ Health Checks
  └─ Automated monitoring
     β”œβ”€ API: HTTP health endpoint
     └─ MCP: Process monitoring

βœ“ Security Scanning
  └─ Trivy in CI/CD
     β”œβ”€ Vulnerability detection
     └─ Automatic SARIF reports

🎯 Mode Selection Decision Tree#

What do you want to do?
β”‚
β”œβ”€ Run ingestion once?
β”‚  └─► CLI Mode
β”‚      docker run terraform-ingest:cli ingest config.yaml
β”‚
β”œβ”€ Expose REST API?
β”‚  └─► API Mode
β”‚      docker run -p 8000:8000 terraform-ingest:api
β”‚
β”œβ”€ Integrate with AI agents?
β”‚  └─► MCP Mode
β”‚      docker run terraform-ingest:mcp
β”‚
└─ Develop & test code?
   └─► Dev Mode
       docker-compose up terraform-ingest-dev
       docker-compose exec terraform-ingest-dev bash

Environment Variable Hierarchy#

Application Configuration
──────────────────────────

Docker Env Vars
     β”‚
     β”œβ”€ TERRAFORM_INGEST_CONFIG
     β”‚  └─ Default: /app/config/config.yaml
     β”‚
     β”œβ”€ API Mode Only
     β”‚  β”œβ”€ UVICORN_HOST (0.0.0.0)
     β”‚  β”œβ”€ UVICORN_PORT (8000)
     β”‚  β”œβ”€ UVICORN_LOG_LEVEL (info)
     β”‚  └─ UVICORN_WORKERS (1)
     β”‚
     └─ MCP Mode Only
        β”œβ”€ TERRAFORM_INGEST_MCP_AUTO_INGEST (true)
        β”œβ”€ TERRAFORM_INGEST_MCP_INGEST_ON_STARTUP (false)
        └─ TERRAFORM_INGEST_MCP_REFRESH_INTERVAL_HOURS (24)

Config File (YAML)
     β”‚
     β”œβ”€ repositories[]
     β”œβ”€ output_dir
     β”œβ”€ clone_dir
     └─ mcp:
        β”œβ”€ auto_ingest
        β”œβ”€ ingest_on_startup
        └─ refresh_interval_hours

πŸ—‚οΈ Project Structure#

terraform-ingest/
β”œβ”€β”€ Dockerfile                          ← Build definition
β”œβ”€β”€ .dockerignore                       ← Build optimizations
β”œβ”€β”€ docker-compose.yml                  ← Service definitions
β”‚
β”œβ”€β”€ docker.md                           ← Quick start guide
β”‚
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ docker_complete.md              ← This summary
β”‚   β”œβ”€β”€ docker_guide.md           ← Full guide (600+ lines)
β”‚   β”œβ”€β”€ docker_quick_ref.md       ← Quick commands
β”‚   β”œβ”€β”€ docker_complete.md        ← Technical details
β”‚   └── DOCKER_CHECKLIST.md             ← Verification
β”‚
β”œβ”€β”€ .github/workflows/
β”‚   └── docker-build.yml                ← CI/CD pipeline
β”‚
β”œβ”€β”€ src/terraform_ingest/
β”‚   β”œβ”€β”€ cli.py                          ← CLI mode
β”‚   β”œβ”€β”€ api.py                          ← API mode
β”‚   β”œβ”€β”€ mcp_service.py                  ← MCP mode
β”‚   β”œβ”€β”€ ingest.py                       ← Core logic
β”‚   └── ...
β”‚
β”œβ”€β”€ pyproject.toml                      ← Python project config
└── config.yaml                         ← Example config

✨ Quick Reference Matrix#

Aspect CLI API MCP Dev
Image terraform-ingest:cli terraform-ingest:api terraform-ingest:mcp terraform-ingest:dev
Entry terraform-ingest uvicorn terraform-ingest-mcp /bin/bash
Port - 8000 stdio -
Duration Short-lived Long-running Long-running Interactive
Use Batch Service AI Development
Command docker run docker run -p docker run docker-compose exec
Docker Compose run --rm up -d up -d up -d then exec

Visual Guide Complete βœ…

For detailed information, see the full documentation files!