grok2api / Dockerfile
FUCAT's picture
fix: align dependencies with upstream (tiktoken, curl-cffi, cryptography, certifi, starlette, tomli-w) and use Python 3.13
3416ed9
Raw
History Blame Contribute Delete
1.88 kB
# Hugging Face Spaces 优化的 Dockerfile for grok2api
# 基于官方项目适配,支持 Persistent Storage
FROM python:3.13-slim-bookworm AS builder
# 安装构建依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
curl \
git \
&& rm -rf /var/lib/apt/lists/*
# 安装 uv(快速的 Python 包管理器)
COPY --from=ghcr.io/astral-sh/uv:0.6 /uv /uvx /bin/
WORKDIR /app
# 复制项目文件
COPY pyproject.toml uv.lock* ./
# 创建虚拟环境并安装依赖
RUN uv venv /opt/venv && \
. /opt/venv/bin/activate && \
uv pip install --no-cache-dir -r pyproject.toml
# ============================================================================
# 运行时镜像
# ============================================================================
FROM python:3.13-slim-bookworm
# 安装运行时依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
wget \
libstdc++6 \
libcurl4 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# 从构建阶段复制虚拟环境
COPY --from=builder /opt/venv /opt/venv
# 复制应用代码
COPY app ./app
COPY scripts ./scripts
COPY config.defaults.toml ./
COPY pyproject.toml ./
# 创建必要的目录
# /data 用于 Hugging Face Persistent Storage
RUN mkdir -p /data /app/logs && \
chmod +x /app/scripts/entrypoint.sh /app/scripts/init_storage.sh
# 设置 Python 路径
ENV PATH="/opt/venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1
# 暴露端口
EXPOSE 8000
# 健康检查
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
CMD wget -qO /dev/null http://127.0.0.1:8000/health || exit 1
# 启动脚本
ENTRYPOINT ["/app/scripts/entrypoint.sh"]
CMD ["sh", "-c", "exec python -m granian --interface asgi --host 0.0.0.0 --port 8000 --workers 1 app.main:app"]