Developing an AI-based Dockerfile generation system
AI-powered Dockerfile generation creates optimal container images based on project analysis. The system determines runtime and dependencies, optimizes layers, and applies security best practices.
Analysis and generation
def generate_dockerfile(project_path: str) -> str:
analyzer = ProjectAnalyzer()
profile = analyzer.analyze(project_path)
prompt = f"""Создай оптимальный Dockerfile для проекта.
Язык: {profile.primary_language}
Runtime: {profile.runtime_version}
Зависимости: {profile.dependencies_file}
Entry point: {profile.entry_point}
Порт: {profile.exposed_port}
Best practices:
- Multi-stage build (отдельный build и runtime stage)
- Минимальный base image (slim/alpine)
- Non-root user
- .dockerignore
- Использование cache для зависимостей (COPY package.json перед COPY .)
- HEALTHCHECK
- Только необходимые файлы в финальном образе"""
return llm.generate(prompt, max_tokens=1000)
Typical result for Python/FastAPI
# Build stage
FROM python:3.11-slim as builder
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential && rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
# Runtime stage
FROM python:3.11-slim
RUN useradd --create-home --shell /bin/bash appuser
WORKDIR /app
COPY --from=builder /install /usr/local
COPY --chown=appuser:appuser . .
USER appuser
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "2"]
Image size optimization
AI analyzes the Dockerfile and suggests optimizations: combining RUN commands for fewer layers, using a lighter base image (slim vs. full → -300MB), removing dev dependencies in the production image, using .dockerignore to exclude unnecessary files.
Vulnerability Scan
After generation, automatic scan via Trivy:
trivy image --exit-code 1 --severity HIGH,CRITICAL myapp:latest
If CRITICAL vulnerabilities are found in the base image, AI suggests updating to a more recent version or changing the base image.







