71 lines
2.1 KiB
Docker
71 lines
2.1 KiB
Docker
# ==============================================================================
|
|
# Agriculture Trading Backoffice (ATB) - Backend Production Dockerfile
|
|
# Multi-stage hardened build for Python 3.12 FastAPI service
|
|
# Standardized per UP-AI v1.5 / tech-profile.yaml
|
|
# ==============================================================================
|
|
|
|
# ---- Stage 1: Build & Dependencies ----
|
|
FROM python:3.12-slim AS builder
|
|
|
|
WORKDIR /build
|
|
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
build-essential \
|
|
libpq-dev \
|
|
curl \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
RUN python -m venv /opt/venv
|
|
ENV PATH="/opt/venv/bin:$PATH"
|
|
|
|
COPY backend/requirements.txt .
|
|
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
|
|
pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
# ---- Stage 2: Hardened Runtime ----
|
|
FROM python:3.12-slim AS runtime
|
|
|
|
LABEL maintainer="ATB Engineering Team"
|
|
LABEL version="1.0.0"
|
|
LABEL description="Agriculture Trading Backoffice (ATB) Core Backend API"
|
|
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PYTHONPATH=/app \
|
|
PATH="/opt/venv/bin:$PATH" \
|
|
PORT=8000
|
|
|
|
# Install runtime dependencies only (libpq for PostgreSQL driver)
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
libpq5 \
|
|
curl \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Create dedicated non-root application user and group
|
|
RUN groupadd -g 10001 appgroup && \
|
|
useradd -u 10001 -g appgroup -s /sbin/nologin -d /home/appuser -m appuser
|
|
|
|
# Copy virtualenv from builder stage
|
|
COPY --from=builder /opt/venv /opt/venv
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy application code and migrations
|
|
COPY --chown=appuser:appgroup backend /app/backend
|
|
COPY --chown=appuser:appgroup sandbox /app/sandbox
|
|
COPY --chown=appuser:appgroup tests /app/tests
|
|
|
|
# Security: switch to non-root user
|
|
USER 10001:10001
|
|
|
|
EXPOSE 8000
|
|
|
|
# Production Healthcheck
|
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
|
CMD curl -f http://127.0.0.1:8000/health || exit 1
|
|
|
|
# Default command starts Uvicorn production server
|
|
CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1", "--log-level", "info"]
|
|
|