Spaces:
Sleeping
Sleeping
| # ============================ | |
| # Build stage | |
| # ============================ | |
| FROM python:3.11-slim AS builder | |
| WORKDIR /app | |
| # Install system dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| gcc \ | |
| g++ \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Create virtual environment | |
| RUN python -m venv /opt/venv | |
| ENV PATH="/opt/venv/bin:$PATH" | |
| # Copy requirements and install dependencies | |
| COPY requirements.txt . | |
| RUN pip install --upgrade pip && \ | |
| pip install --no-cache-dir -r requirements.txt | |
| # ============================ | |
| # Production stage | |
| # ============================ | |
| FROM python:3.11-slim | |
| WORKDIR /app | |
| # Install runtime dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| curl \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy virtual environment from builder | |
| COPY --from=builder /opt/venv /opt/venv | |
| ENV PATH="/opt/venv/bin:$PATH" | |
| # Create non-root user | |
| RUN groupadd -r appuser && useradd -r -g appuser appuser | |
| # Create envs directory and copy environment files | |
| RUN mkdir -p /app/envs | |
| COPY --chown=appuser:appuser envs/.env.production /app/envs/.env.production | |
| COPY --chown=appuser:appuser envs/.env.main /app/envs/.env.main | |
| # Copy application code | |
| COPY --chown=appuser:appuser . . | |
| USER appuser | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \ | |
| CMD curl -f http://localhost:7860/health || exit 1 | |
| EXPOSE 7860 | |
| # Start the application | |
| CMD ["sh", "-c", "ACTIVE_ENV=production uvicorn app.main:app --host 0.0.0.0 --port 7860 --timeout-keep-alive 30"] |