# 多阶段构建Dockerfile for MCP Server FROM python:3.11-slim AS builder WORKDIR /app # 安装系统依赖 RUN apt-get update && apt-get install -y \ gcc \ g++ \ libpq-dev \ && rm -rf /var/lib/apt/lists/* # 安装Python依赖 COPY requirements.txt . RUN pip install --no-cache-dir --user -r requirements.txt \ -i https://pypi.tuna.tsinghua.edu.cn/simple/ \ --trusted-host pypi.tuna.tsinghua.edu.cn # 最终镜像 FROM python:3.11-slim WORKDIR /app # 安装运行时依赖 RUN apt-get update && apt-get install -y \ libpq5 \ curl \ && rm -rf /var/lib/apt/lists/* # 从builder复制Python包 COPY --from=builder /root/.local /root/.local # 复制应用代码 COPY . . # 确保Python能找到用户安装的包 ENV PATH=/root/.local/bin:$PATH # 创建日志目录 RUN mkdir -p /app/logs # 健康检查 (开发环境使用较长间隔) HEALTHCHECK --interval=120s --timeout=10s --start-period=40s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1 # 暴露端口 EXPOSE 8000 # 启动命令(使用单 worker 避免数据库初始化竞态条件) CMD ["python3", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]