# Multi-stage build for optimization FROM python:3.9-slim as builder # Install system dependencies for building RUN apt-get update && apt-get install -y \ build-essential \ gcc \ g++ \ && rm -rf /var/lib/apt/lists/* # Create virtual environment RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Copy and install requirements COPY requirements.txt /tmp/requirements.txt COPY webui/requirements.txt /tmp/webui_requirements.txt # Install main project dependencies RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r /tmp/requirements.txt # Install webui dependencies RUN pip install --no-cache-dir -r /tmp/webui_requirements.txt # Install production WSGI server RUN pip install --no-cache-dir gunicorn # Production stage FROM python:3.9-slim # Install runtime dependencies RUN apt-get update && apt-get install -y \ curl \ && rm -rf /var/lib/apt/lists/* # Create non-root user RUN useradd -m -u 1000 user && \ mkdir -p /app && \ chown -R user:user /app # Copy virtual environment from builder stage COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Set working directory WORKDIR /app # Copy application code COPY --chown=user:user . /app # Make startup script executable RUN chmod +x /app/webui/docker_start.sh # Switch to non-root user USER user # Expose the Flask app port EXPOSE 7860 # Add health check HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD curl -f http://localhost:7860/ || exit 1 # Set environment variables ENV PYTHONPATH=/app ENV FLASK_APP=webui/app.py ENV FLASK_ENV=production ENV PORT=7860 ENV HF_HOME=/home/user/.cache/huggingface ENV HUGGINGFACE_HUB_CACHE=/home/user/.cache/huggingface ENV HF_HUB_DISABLE_TELEMETRY=1 ENV PIP_NO_CACHE_DIR=1 # Run the Flask app directly CMD ["python", "webui/app.py"]