# 使用Python 3.9作为基础镜像 FROM python:3.9-slim # 创建非root用户(Hugging Face Spaces要求) RUN useradd -m -u 1000 user # 设置工作目录 WORKDIR /code # 安装系统依赖 RUN apt-get update && apt-get install -y \ build-essential \ curl \ && rm -rf /var/lib/apt/lists/* # 复制requirements.txt COPY --chown=user requirements.txt . # 安装Python依赖 RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # 切换到非root用户 USER user # 设置环境变量 ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH \ PYTHONPATH=/code # 复制应用代码 COPY --chown=user . . # 暴露端口 EXPOSE 7860 # 启动服务 CMD ["python", "app.py"]