FROM python:3.12-slim # 设置工作目录 WORKDIR /app # 设置环境变量 ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PORT=7860 \ FLASK_ENV=production # 安装系统依赖(如需要) RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ && rm -rf /var/lib/apt/lists/* # 复制requirements文件 COPY requirements.txt . # 安装Python依赖 RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # 复制应用文件 COPY . . # 创建日志目录并设置权限 RUN mkdir -p logs && chmod 777 logs # 暴露7860端口(Hugging Face Spaces要求) EXPOSE 7860 # 健康检查 HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD curl -f http://localhost:7860/api/health || exit 1 # 使用 Gunicorn 启动 Flask 应用(生产环境推荐) # 使用配置文件以显示客户端真实 IP CMD ["gunicorn", "-c", "gunicorn_config.py", "app:app"] # 如果想使用 Flask 内置服务器(开发/测试用),使用下面这行: # CMD ["python3.12", "app.py"]