# Stage 1: Builder - Install dependencies FROM python:3.11-slim AS builder WORKDIR /app # Install Poetry RUN pip install --no-cache-dir poetry==1.8.0 # Configure Poetry for non-interactive installation RUN poetry config virtualenvs.create false # Copy only dependency files first (cache layer) COPY pyproject.toml poetry.lock ./ # Install only production dependencies RUN poetry install --only main --no-interaction --no-ansi --no-root # Stage 2: Runtime - Minimal production image FROM python:3.11-slim AS runtime WORKDIR /app # 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 useradd --create-home --shell /bin/bash appuser # Create directories with proper ownership RUN mkdir -p /app/logs /app/models && chown -R appuser:appuser /app # Copy application code (separate layer for faster rebuilds) COPY --chown=appuser:appuser app/ ./app/ # Switch to non-root user USER appuser # Environment configuration ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 ENV PYTHONPATH=/app # Hugging Face model cache directory ENV HF_HOME=/app/models ENV TRANSFORMERS_CACHE=/app/models ENV SENTENCE_TRANSFORMERS_HOME=/app/models ENV WHISPER_MODELS_DIR=/app/models # Disable file logging in container ENV DISABLE_FILE_LOGGING=true EXPOSE 7860 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/health')" || exit 1 CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]