Docker打包rust时版本依赖问题

rust程序在服务器上能打包成功,但在Dockerfile运行时始终报依赖错误,涉及版本太多即使手动改.lock文件效果也不理想,切换镜像无效果

> [4/4] RUN cargo install --path .:                                            
0.426   Installing backend v0.1.0 (/backend)                                    
0.820     Updating `ustc` index                                                 
293.5 error: failed to compile `backend v0.1.0 (/backend)`, intermediate artifacts can be found at `/backend/target`                                            
293.5 
293.5 Caused by:
293.5   failed to select a version for the requirement `ahash = "^0.8"`
293.5   candidate versions found which didn't match: 0.4.8, 0.4.1, 0.4.0, ...
293.5   location searched: `ustc` index (which is replacing registry `crates-io`)
293.5   required by package `actix-web v4.5.1`
293.5       ... which satisfies dependency `actix-web = "^4.5.1"` of package `backend v0.1.0 (/backend)`
293.5   perhaps a crate was updated and forgotten to be re-vendored?
------

最后查明问题原因是阿里云docker镜像中的rust最新版本太低,命令行输入:

docker pull rust:latest
docker run --rm rust:latest rustc --version

得到版本只有1.57.0,更服务器上使用1.79.0相差太远

rustc 1.57.0 (f1edd0429 2021-11-29)

解决方案:在Dockerfile中升级rust到指定版本: RUN rustup toolchain install 1.79.0 && rustup default 1.79.0

FROM rust:latest
  
WORKDIR /backend

RUN rustup toolchain install 1.79.0 && rustup default 1.79.0

COPY . .

RUN cargo install --path .

CMD ["backend"]
posted @ 2024-07-05 15:07  李思默  阅读(225)  评论(0)    收藏  举报