FROM rustlang/rust:nightly-bookworm-slim as builder WORKDIR /app # 安装依赖 RUN apt-get update && \ apt-get install -y --no-install-recommends \ build-essential protobuf-compiler pkg-config libssl-dev nodejs npm openssl \ && rm -rf /var/lib/apt/lists/* # 先复制Cargo配置和构建相关文件 COPY Cargo.toml Cross.toml* build.rs* .gitattributes* ./ COPY .cargo /.cargo/ # 创建基本src目录结构以利用缓存 RUN mkdir -p src && \ echo "fn main() {println!(\"placeholder\");}" > src/main.rs # 尝试获取依赖(如果可能) RUN cargo fetch || true # 现在复制真正的源代码 COPY src ./src/ COPY tests ./tests/ COPY scripts ./scripts/ COPY static ./static/ COPY tools ./tools/ # 构建 RUN ARCH=$(uname -m) && \ if [ "$ARCH" = "x86_64" ]; then \ TARGET_CPU="x86-64-v2"; \ elif [ "$ARCH" = "aarch64" ]; then \ TARGET_CPU="neoverse-n1"; \ else \ echo "Unsupported architecture: $ARCH" && exit 1; \ fi && \ RUSTFLAGS="-C link-arg=-s -C target-cpu=$TARGET_CPU" cargo +nightly build --release # 查找生成的二进制文件并复制到/app/cursor-api RUN find ./target/release -maxdepth 1 -type f -executable -not -name "*.d" -exec cp {} /app/cursor-api \; || \ (echo "No executable binary found in target/release" && ls -la ./target/release && exit 1) # 运行阶段 FROM debian:bookworm-slim WORKDIR /app ENV TZ=Asia/Shanghai RUN apt-get update && \ apt-get install -y --no-install-recommends \ ca-certificates tzdata openssl \ && rm -rf /var/lib/apt/lists/* && \ groupadd -r cursorapi && useradd -r -g cursorapi cursorapi COPY --from=builder /app/cursor-api . RUN chown -R cursorapi:cursorapi /app && \ chmod +x /app/cursor-api ENV PORT=3000 EXPOSE ${PORT} USER cursorapi CMD ["./cursor-api"]