FROM python:3.11-slim # 设置工作目录 WORKDIR /app # 安装系统依赖 RUN apt-get update && apt-get install -y \ build-essential \ curl \ && rm -rf /var/lib/apt/lists/* # 复制依赖文件 COPY requirements.txt . # 安装Python依赖 RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # 复制应用代码 COPY . . # 设置环境变量 ENV HOST=0.0.0.0 ENV PORT=7860 ENV PYTHONUNBUFFERED=1 ENV DEBIAN_FRONTEND=noninteractive # 删除敏感文件 RUN rm -f config.json password.txt # 创建非root用户 RUN useradd -m -u 1000 user && \ chown -R user:user /app # 切换到非root用户 USER user # 暴露端口(Hugging Face默认使用7860端口) EXPOSE 7860 # 健康检查 HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD curl -f http://localhost:7860/health || exit 1 # 启动命令 CMD ["python", "app.py"]