# New API 中转 —— Hugging Face Spaces (Docker SDK) # 直接从 new-api 官方 GitHub 源码构建,不依赖 Docker Hub 预构建镜像。 # 构建步骤复刻自官方 Dockerfile(前端 bun,后端 go)。 # ---------- 0. 拉取源码 ---------- FROM alpine/git AS source # 可改成具体 tag(如 v0.x.x)来锁版本,默认拉取最新 main 分支 ARG NEW_API_REF=main WORKDIR /src RUN git clone --depth 1 --branch ${NEW_API_REF} https://github.com/QuantumNous/new-api.git . \ && rm -rf .git # ---------- 1. 前端构建 (default + classic) ---------- FROM oven/bun:1 AS builder WORKDIR /build COPY --from=source /src/ ./ WORKDIR /build/web RUN bun install --frozen-lockfile RUN cd default \ && DISABLE_ESLINT_PLUGIN='true' VITE_REACT_APP_VERSION=$(cat /build/VERSION) bun run build RUN cd classic \ && VITE_REACT_APP_VERSION=$(cat /build/VERSION) bun run build # ---------- 2. 后端构建 (Go) ---------- FROM golang:1.26.1-alpine AS builder2 ENV GO111MODULE=on CGO_ENABLED=0 ARG TARGETOS ARG TARGETARCH ENV GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} ENV GOEXPERIMENT=greenteagc WORKDIR /build COPY --from=source /src/ ./ COPY --from=builder /build/web/default/dist ./web/default/dist COPY --from=builder /build/web/classic/dist ./web/classic/dist RUN go mod download RUN go build -ldflags "-s -w -X 'github.com/QuantumNous/new-api/common.Version=$(cat VERSION)'" -o new-api # ---------- 3. 运行镜像 ---------- FROM debian:bookworm-slim RUN apt-get update \ && apt-get install -y --no-install-recommends ca-certificates tzdata libasan8 wget \ && rm -rf /var/lib/apt/lists/* \ && update-ca-certificates COPY --from=builder2 /build/new-api / # Hugging Face Spaces 通过 app_port(=7860) 转发流量 ENV PORT=7860 # SQLite 落到 HF 持久化存储挂载点 /data(Space 设置里开 Persistent storage) ENV SQLITE_PATH=/data/new-api.db EXPOSE 7860 WORKDIR /data ENTRYPOINT ["/new-api"]