# ========================================== # SAAP Backend Dockerfile (Multi-Stage Build) # Python 3.11 + FastAPI + PostgreSQL # ========================================== # ========================================== # Stage 1: Builder (Dependencies Installation) # ========================================== FROM python:3.11-slim AS builder LABEL maintainer="SATWARE AG " LABEL description="SAAP Backend - satware Autonomous Agent Platform" # Set working directory WORKDIR /app # Install system dependencies for building Python packages RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ g++ \ libpq-dev \ && rm -rf /var/lib/apt/lists/* # Copy requirements first for better caching COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # ========================================== # Stage 2: Runtime (Minimal Production Image) # ========================================== FROM python:3.11-slim AS runtime # Set working directory WORKDIR /app # Install runtime dependencies only RUN apt-get update && apt-get install -y --no-install-recommends \ libpq5 \ curl \ && rm -rf /var/lib/apt/lists/* # Copy installed packages from builder COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages COPY --from=builder /usr/local/bin /usr/local/bin # Create non-root user for security RUN groupadd -r saap && useradd -r -g saap saap # Copy application code COPY --chown=saap:saap . . # Create necessary directories RUN mkdir -p logs && chown -R saap:saap logs # Switch to non-root user USER saap # Environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PYTHONPATH=/app # Expose port EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1 # Run application CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]