๐ Docker Multi-Stage Implementation Complete#
โ Summary#
I've successfully created a comprehensive multi-stage Docker setup for terraform-ingest that supports three distinct execution modes with full documentation and CI/CD pipeline.
๐ Files Created (9 Total)#
Core Docker Files (3)#
-
Dockerfile(148 lines) - 6 build stages: builder, runtime-base, cli, api, mcp, dev - Multi-platform ready (amd64, arm64) - Health checks and environment configuration - Optimized layer caching strategy -
.dockerignore(50+ files excluded) - Optimizes build context - Reduces build time and size - Excludes test, git, and deployment files -
docker-compose.yml(145 lines) - 4 services with selective profiles - Volume mounts for configuration and data - Environment configuration for each mode - SSH key support for private repositories
Documentation Files (5)#
-
docker.md(Quick Start Guide) - ๐ Quick start for all three modes - ๐ Common tasks - ๐ก Tips and tricks - ๐ง Troubleshooting basics -
docker_guide.md(Comprehensive Guide) - ๐ 600+ lines of detailed documentation - All three execution modes explained - API endpoint documentation - Volume management guide - Production deployment examples - Troubleshooting section -
docker_quick_ref.md(Quick Reference) - โก Common commands - ๐ Options table - Environment variables - Docker Compose shortcuts - Health check commands -
docker_complete.md(Implementation Details) - ๐ What was built and why - Build optimization strategies - Security features explained - GitHub Actions workflow details - Production deployment examples (Kubernetes)
CI/CD Files (1)#
.github/workflows/docker-build.yml(141 lines) - โจ Multi-platform builds (Linux amd64, arm64) - ๐ Security scanning with Trivy - ๐งช Automated testing - ๐ค Push to GitHub Container Registry (GHCR) - ๐ท๏ธ Semantic versioning support
๐ฏ Execution Modes#
1. CLI Mode (terraform-ingest:cli)#
Use for: One-off ingestion tasks, batch processing, CI/CD pipelines
docker run -v config.yaml:/app/config/config.yaml \
-v output:/app/output \
terraform-ingest:cli ingest /app/config/config.yaml
Features: - Full Click CLI support - Cleanup option - SSH key support - Custom configurations
2. API Mode (terraform-ingest:api)#
Use for: REST service, long-running applications, Kubernetes
docker run -p 8000:8000 terraform-ingest:api
Features: - FastAPI REST endpoints - Swagger UI documentation - Health check endpoints - Multi-worker support - Configurable logging
Endpoints:
- GET / - API info
- GET /health - Health check
- POST /ingest - Ingest repositories
- POST /analyze - Analyze single repo
- GET /docs - Swagger UI
- GET /redoc - ReDoc documentation
3. MCP Mode (terraform-ingest:mcp)#
Use for: AI agent integration, LLM access, model context protocol
docker run terraform-ingest:mcp
Features: - FastMCP server - Stdio-based protocol - Auto-ingestion support - Periodic refresh capability - List repositories tool - Search modules tool
๐ Quick Start#
Build All Images#
docker-compose build
Run in Your Chosen Mode#
CLI - Run Once:
docker-compose run --rm terraform-ingest-cli ingest /app/config/config.yaml
API - Start Service:
docker-compose up -d terraform-ingest-api
curl http://localhost:8000/health
MCP - Start Agent Server:
docker-compose up -d terraform-ingest-mcp
Development - Interactive:
docker-compose up -d terraform-ingest-dev
docker-compose exec terraform-ingest-dev bash
๐ Image Specifications#
| Image | Size | Base | Purpose |
|---|---|---|---|
| cli | ~350MB | python:3.13-slim | CLI commands |
| api | ~350MB | python:3.13-slim | REST service |
| mcp | ~350MB | python:3.13-slim | MCP server |
| dev | ~550MB | python:3.13-slim | Development |
Sizes are estimates and depend on build cache and system
๐ Security Features#
โ
Minimal Base Image: python:3.13-slim only (~150MB)
โ
Multi-Stage Builds: No build tools in production
โ
Read-Only Mounts: Configuration files mounted as read-only
โ
SSH Key Isolation: Credentials kept secure via volume mounts
โ
Health Checks: Automated health monitoring
โ
Security Scanning: Trivy scans in GitHub Actions
โ
No Hardcoded Secrets: Environment-based configuration
โ
Minimal Permissions: No sudo or unnecessary privileges
๐ ๏ธ Build Optimization#
Layer Caching Strategy: 1. Dependencies cached separately from code 2. Build tools excluded from production 3. Multi-platform support (amd64, arm64) 4. GitHub Actions cache for faster CI/CD
Size Optimization:
- Slim base image (vs full Python)
- Multi-stage builds eliminate build tools
- .dockerignore excludes unnecessary files
- Minimal runtime dependencies only
๐ Docker Compose Features#
Service Profiles:
docker-compose --profile cli run --rm terraform-ingest-cli
docker-compose --profile api up
docker-compose --profile mcp up
docker-compose --profile dev up
Volume Management: - Config mount (read-only) - Output directory - Repos cache - SSH keys (optional) - Git config (optional)
Environment Configuration: - All modes configurable via env vars - No hardcoded values - Easy customization
๐ GitHub Actions CI/CD#
Triggers: - Push to main/develop branches - Git tags (semantic versioning) - Manual workflow dispatch - Pull requests (testing only)
Jobs: 1. Build: Compiles all targets, multi-platform 2. Test: Basic functionality tests 3. Security: Trivy vulnerability scanning
Output: - Images pushed to GitHub Container Registry (GHCR) - Semantic version tags - SHA-based tags - Latest tags for default branch - Security reports in GitHub Security tab
๐ Documentation Structure#
docker.md โ Start here!
โโโ docker_guide.md โ Full guide (600+ lines)
โโโ docker_quick_ref.md โ Quick reference
โโโ docker_complete.md โ Implementation details
โโโ docs/DOCKER_CHECKLIST.md โ Verification & next steps
โโโ Dockerfile โ Source
docker-compose.yml
.dockerignore
.github/workflows/docker-build.yml
๐ฏ Next Steps#
1. Build Locally#
docker-compose build
2. Test Each Mode#
# CLI
docker-compose run --rm terraform-ingest-cli --help
# API
docker-compose up -d terraform-ingest-api
curl http://localhost:8000/docs
# MCP
docker-compose up -d terraform-ingest-mcp
# Dev
docker-compose up -d terraform-ingest-dev
docker-compose exec terraform-ingest-dev pytest tests/
3. Configure#
- Create
config.yamlwith your repositories - Mount SSH keys if needed:
~/.ssh:/root/.ssh:ro - Adjust environment variables as needed
4. Deploy#
- Local: Use
docker-compose - Kubernetes: Use provided manifests
- Docker Swarm: Use
docker-compose - Cloud: Push to GHCR and deploy from registry
5. Monitor#
- Check health:
curl http://localhost:8000/health - View logs:
docker-compose logs -f - Monitor resources:
docker stats
๐ก Key Highlights#
For CLI Users#
โ
One-line command execution
โ
Cleanup option
โ
Private repo support
โ
Scheduled job friendly
For API Users#
โ
Production-grade REST service
โ
Full API documentation
โ
Health checks
โ
Scalable worker processes
For MCP Users#
โ
AI agent integration
โ
Automatic ingestion
โ
Periodic refresh
โ
LLM tool support
For Developers#
โ
Full dev environment
โ
Testing tools included
โ
Code formatting
โ
Interactive shell
๐ Integration Examples#
With Kubernetes#
apiVersion: apps/v1
kind: Deployment
metadata:
name: terraform-ingest-api
spec:
template:
spec:
containers:
- name: api
image: ghcr.io/zloeber/terraform-ingest:api-latest
ports:
- containerPort: 8000
livenessProbe:
httpGet:
path: /health
port: 8000
With AI Agents (Claude, etc.)#
{
"terraform-ingest-mcp": {
"command": "docker",
"args": ["run", "--rm", "-v", "/path/to/output:/app/output", "terraform-ingest:mcp"]
}
}
With GitHub Actions#
- name: Ingest Terraform Modules
run: |
docker run \
-v ${{ github.workspace }}/config.yaml:/app/config/config.yaml \
-v ${{ github.workspace }}/output:/app/output \
terraform-ingest:cli ingest /app/config/config.yaml
๐ Documentation Navigation#
| Need | File | Purpose |
|---|---|---|
| Quick start | docker.md |
Get started immediately |
| All details | docker_guide.md |
Comprehensive guide |
| Commands | docker_quick_ref.md |
Copy-paste commands |
| How it works | docker_complete.md |
Technical details |
| Verify | docs/DOCKER_CHECKLIST.md |
Check everything |
| Build source | Dockerfile |
See actual code |
โจ Features Summary#
- โ 3 Execution Modes: CLI, API, MCP
- โ Multi-Stage Build: Optimized images
- โ CI/CD Pipeline: GitHub Actions ready
- โ Security Scanning: Trivy integration
- โ Docker Compose: Easy local development
- โ Health Checks: Automatic monitoring
- โ Production Ready: Kubernetes deployments
- โ Comprehensive Docs: 5 documentation files
- โ Private Repo Support: SSH key mounting
- โ Multi-Platform: amd64 and arm64 support
๐ Learning Resources#
- Docker Official Documentation
- Docker Compose Guide
- FastAPI Documentation
- FastMCP Documentation
- Kubernetes Deployments
๐ Quick Troubleshooting#
Build fails?
docker build --no-cache -t terraform-ingest:cli --target cli .
Container won't start?
docker logs <container_name>
Permission denied with SSH?
chmod 600 ~/.ssh/id_rsa && chmod 700 ~/.ssh
Port already in use?
docker run -p 8001:8000 terraform-ingest:api # Use different port
For more help, see docker_guide.md
๐ What You Can Do Now#
- โ
Run
terraform-ingestas a CLI tool - โ Run as a REST API service
- โ Integrate with AI agents via MCP
- โ Deploy to Kubernetes
- โ Deploy to Docker Swarm
- โ Use in GitHub Actions
- โ Develop with full dev environment
- โ Test with Pytest
- โ Format code with Black
- โ Lint with Flake8
๐ Support#
- Full Guide:
docker_guide.md - Quick Ref:
docker_quick_ref.md - Implementation:
docker_complete.md - Checklist:
docs/DOCKER_CHECKLIST.md
Status: โ COMPLETE & READY TO USE
Start with docker.md for quick start, or dive into docker_guide.md for comprehensive details!