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

Docker容器中使用Cargo构建时遇注册表更新网络故障如何解决?

问题

尝试在Docker容器中用cargo构建程序失败,Dockerfile内容如下:

# Compile KissMP
FROM --platform=arm64 ubuntu:20.04

# Install dependencies
RUN apt-get update
RUN apt-get install -y cargo
RUN apt-get install -y git

# Clone & Build KissMP
WORKDIR /build
RUN git clone -b lib https://github.com/vulcan-dev/KISS-multiplayer.git
WORKDIR /KISS-multiplayer
WORKDIR /build/KISS-multiplayer/kissmp-server/
RUN cargo build --release

构建时出现错误:

#24 2.299     Updating crates.io index
#24 681.7 error: failed to get `anyhow` as a dependency of package `kissmp-bridge v0.5.0 (/build/KISS-multiplayer/kissmp-bridge)`
#24 681.7 
#24 681.7 Caused by:
#24 681.7   failed to load source for dependency `anyhow`
#24 681.7 
#24 681.7 Caused by:
#24 681.7   Unable to update registry `crates-io`
#24 681.7 
#24 681.7 Caused by:
#24 681.7   failed to fetch `https://github.com/rust-lang/crates.io-index`
#24 681.7 
#24 681.7 Caused by:
#24 681.7   network failure seems to have happened
#24 681.7   if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
#24 681.7   https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli
#24 681.7 
#24 681.7 Caused by:
#24 681.7   SSL error: unknown error; class=Ssl (16)

注:无法使用Alpine系统。

解决方法

针对SSL错误导致的cargo拉取依赖失败,可尝试以下方案:

  • 更新系统SSL证书并补全依赖
    Ubuntu 20.04的默认证书可能存在问题,同时安装openssl相关工具可避免SSL验证失败。修改Dockerfile的依赖安装部分:

    RUN apt-get update && apt-get install -y cargo git ca-certificates libssl-dev && update-ca-certificates
    

    合并RUN命令还能减少镜像层数,优化构建效率。

  • 让cargo使用系统git拉取索引
    按照错误提示配置cargo,强制使用系统git命令而非内置库拉取crates.io索引,在cargo build --release前添加:

    RUN cargo config set net.git-fetch-with-cli true
    
  • 更换crates.io镜像源
    若网络访问官方源不稳定,可配置国内镜像源加速。在Dockerfile中添加:

    RUN echo '[source.crates-io]' > ~/.cargo/config.toml \
        && echo 'replace-with = "tuna"' >> ~/.cargo/config.toml \
        && echo '[source.tuna]' >> ~/.cargo/config.toml \
        && echo 'registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"' >> ~/.cargo/config.toml
    
  • 修复Dockerfile路径错误
    原Dockerfile中多余的WORKDIR /KISS-multiplayer会导致路径混乱,删除该行后保持路径正确:

    WORKDIR /build
    RUN git clone -b lib https://github.com/vulcan-dev/KISS-multiplayer.git
    WORKDIR /build/KISS-multiplayer/kissmp-server/
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 10:45:37