FROM python:3.14-slim WORKDIR /app # Security and performance environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ PIP_DISABLE_PIP_VERSION_CHECK=1 # Install security updates RUN apt-get update && \ apt-get upgrade -y && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # Create non-root user with specific UID RUN useradd -m -u 1000 -s /bin/bash user # Install dependencies as root COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # Copy application code and set ownership COPY --chown=user:user . . # Switch to non-root user USER user # Set PATH for user ENV PATH="/home/user/.local/bin:$PATH" \ HOME="/home/user" # Health check (respect Hugging Face PORT env) HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \ CMD python3 -c "import os, requests; port=os.environ.get('PORT','7860'); requests.get(f'http://localhost:{port}/health', timeout=5)" # Expose port 7860 (Hugging Face Spaces default) EXPOSE 7860 # Start server with production settings # Note: default workers=1 is safer on low-memory HF hardware; override via WEB_CONCURRENCY. CMD ["sh", "-c", "uvicorn server:app --host 0.0.0.0 --port ${PORT:-7860} --log-level ${LOG_LEVEL:-info} --no-access-log --workers ${WEB_CONCURRENCY:-1}"]