You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

两阶段Rust Docker镜像编译chromiumoxide_cdp失败求助

编译chromiumoxide_cdp时SIGKILL错误的解决建议

我在搭建两阶段Docker镜像过程中,遇到chromiumoxide_cdp编译失败,错误表现为rustc进程被SIGKILL杀死,具体错误信息如下:

#16 798.3 error: could not compile `chromiumoxide_cdp`
#16 798.3
#16 798.3 Caused by:
#16 798.3   process didn't exit successfully: `rustc --crate-name chromiumoxide_cdp --edition=2021 /usr/local/cargo/git/checkouts/chromiumoxide-b0a6fd9b32d92502/1eb7732/chromiumoxide_cdp/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=4618abc3b634c579 -C extra-filename=-4618abc3b634c579 --out-dir /usr/src/app/target/x86_64-unknown-linux-musl/release/deps --target x86_64-unknown-linux-musl -L dependency=/usr/src/app/target/x86_64-unknown-linux-musl/release/deps -L dependency=/usr/src/app/target/release/deps --extern chromiumoxide_pdl=/usr/src/app/target/x86_64-unknown-linux-musl/release/deps/libchromiumoxide_pdl-3f92e4f213ea525d.rmeta --extern chromiumoxide_types=/usr/src/app/target/x86_64-unknown-linux-musl/release/deps/libchromiumoxide_types-99e1b123e0ccb7f5.rmeta --extern serde=/usr/src/app/target/x86_64-unknown-linux-musl/release/deps/libserde-ddfc800b9403f606.rmeta --extern serde_json=/usr/src/app/target/x86_64-unknown-linux-musl/release/deps/libserde_json-dc59c7191ae94ed7.rmeta --cap-lints allow` (signal: 9, SIGKILL: kill)
------
process "/bin/sh -c cargo build --target x86_64-unknown-linux-musl --release" did not complete successfully: exit code: 101

我的Cargo依赖配置:

tokio = { version = "1.12.0", features = ["full"] }
axum = { version = "0.6.18", features = ["ws"] }
anyhow = "1.0.71"
paris = { version = "1.5.15", features = ["no_logger", "macros"] }
once_cell = "1.17.1"
reqwest = { version = "0.11.17" }
serde = { version = "1.0.163", features = ["derive"]}
scraper = "0.16.0"
serde_json = "1.0.96"
chromiumoxide = { git = "https://github.com/mattsse/chromiumoxide", features = ["tokio-runtime", "_fetcher-rusttls-tokio"], default-features = false, branch = "main"}
base64 = "0.21.0"

futures = "0.3.28" # needed for chrome oxide tokio handler thread
openssl = { version = "0.10.52", features = ["vendored"] }

Dockerfile内容:

################
##### Builder
FROM rust:slim as builder

WORKDIR /usr/src

# Create blank project
RUN USER=root cargo new app

# We want dependencies cached, so copy those first.
COPY Cargo.toml Cargo.lock /usr/src/app/

# Set the working directory
WORKDIR /usr/src/app

RUN apt update && apt install -y perl make musl-tools

## Install target platform (Cross-Compilation) --> Needed for Alpine
RUN rustup target add x86_64-unknown-linux-musl

# This is a dummy build to get the dependencies cached.
RUN cargo build --target x86_64-unknown-linux-musl --release

# Now copy in the rest of the sources
COPY src /usr/src/app/src/

## Touch main.rs to prevent cached release build
RUN touch /usr/src/app/src/main.rs

# This is the actual application build.
RUN cargo build --target x86_64-unknown-linux-musl --release

################
##### Runtime
FROM alpine:latest AS runtime

# Copy application binary from builder image
COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/app /usr/local/bin

EXPOSE 8000

# Run the application
CMD ["/usr/local/bin/app"]

解决建议

  • 补充编译内存:SIGKILL通常是内存不足导致,可在builder阶段添加swap分区缓解:
    RUN fallocate -l 4G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
    
  • 限制编译线程:减少rustc并行编译的线程数,降低内存占用,修改构建命令为:
    RUN cargo build --target x86_64-unknown-linux-musl --release --jobs 1
    
  • 改用稳定依赖版本:避免使用main分支的未稳定代码,替换chromiumoxide依赖为指定版本:
    chromiumoxide = { version = "0.12.0", features = ["tokio-runtime", "_fetcher-rusttls-tokio"], default-features = false }
    
  • 调整镜像架构:放弃musl交叉编译,将runtime阶段改为基于Debian的镜像,直接构建原生二进制:
    # 修改Runtime阶段
    FROM debian:bookworm-slim AS runtime
    COPY --from=builder /usr/src/app/target/release/app /usr/local/bin
    
    同时删除builder中的rustup target add x86_64-unknown-linux-musl和构建命令中的--target x86_64-unknown-linux-musl参数。

内容的提问来源于stack exchange,提问作者Cooper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 08:25:36