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
相关产品推荐
相关产品推荐

