问题描述
有没有办法告诉 Cargo 安装和构建我的所有依赖项,但不要尝试构建我的应用程序?
Is there a way to tell Cargo to install and build all my dependencies, but not attempt to build my application?
我认为 cargo install
会这样做,但实际上它也一直用于构建我的应用程序.我想进入一种状态,其中 cargo build
会找到所有准备使用的依赖项,但不触及 /src
目录.
I thought cargo install
would do that, but it actually goes all the way to building my app too. I want to get to a state where cargo build
would find all dependencies ready to use, but without touching the /src
directory.
我真正想要实现的目标:
我正在尝试为 Rust 应用程序构建 Docker 映像,我想在其中执行以下步骤:
I'm trying to build a Docker image for a Rust application, where I'd like to do the following steps:
构建时间(docker build .
):
- 导入安装了 rust 工具的 docker 镜像
- 添加我的 Cargo.toml 和 Cargo.lock 文件
- 下载并构建所有依赖项
- 将我的源目录添加到图像中
- 构建我的源代码
运行时间(docker run ...
):
- 运行应用程序
我已经尝试了以下 Dockerfile
,但指示的步骤也构建了我的应用程序(由于源目录尚不存在,这当然失败了):
I've tried the following Dockerfile
, but the indicated step builds my application as well (which of course fails since the source directory isn't there yet):
FROM jimmycuadra/rust
ADD Cargo.toml /source
ADD Cargo.lock /source
RUN cargo install # <-- failure here
ADD src /source/src
RUN cargo build
ENTRYPOINT cargo run
我想将安装依赖项步骤与实际构建我的应用程序分开的原因是,如果我不更改依赖项,我希望 Docker 能够使用已安装和构建的所有依赖项的缓存映像.因此,我不能ADD/src/source/src
直到之后安装依赖项,因为当我更改自己的代码时,这会使缓存的图像无效.
The reason I want to separate the install dependencies step from actually building my application, is that if I don't change the dependencies, I want Docker to be able use a cached image with all dependencies already installed and built. Thus, I can't ADD /src /source/src
until after installing the dependecies, as that would invalidate the cached image when I change my own code.
推荐答案
据我所知,没有对仅在 Cargo 中构建依赖项的本地支持.有一个悬而未决的问题.如果你可以向 Cargo 提交一些东西来完成它,或者创建一个第三方 Cargo 插件,我不会感到惊讶.我也希望 cargo doc
有这个功能,当我自己的代码太坏而无法编译时;-)
There is no native support for building just the dependencies in Cargo, as far as I know. There is an open issue for it. I wouldn't be surprised if you could submit something to Cargo to accomplish it though, or perhaps create a third-party Cargo addon. I've wanted this functionality for cargo doc
as well, when my own code is too broken to compile ;-)
但是,我维护的 Rust 游乐场 确实实现了您的最终目标.有一个 基础 Docker 容器,用于安装 Rustup 并复制到Cargo.toml
带有可用于游乐场的所有板条箱.构建步骤创建了一个空白项目(使用src/lib.rs
),然后调用 cargo build
和 cargo build --release
来编译 crate:
However, the Rust playground that I maintain does accomplish your end goal. There's a base Docker container that installs Rustup and copies in a Cargo.toml
with all of the crates available for the playground. The build steps create a blank project (with a dummy src/lib.rs
), then calls cargo build
and cargo build --release
to compile the crates:
RUN cd / && \
cargo new playground
WORKDIR /playground
ADD Cargo.toml /playground/Cargo.toml
RUN cargo build
RUN cargo build --release
RUN rm src/*.rs
所有下载的 crate 都存储在 Docker 镜像的 $HOME/.cargo
目录中,所有构建的 crate 都存储在应用程序 target/{debug,release}代码> 目录.
All of the downloaded crates are stored in the Docker image's $HOME/.cargo
directory and all of the built crates are stored in the applications target/{debug,release}
directories.
稍后,真正的源文件被复制到容器中,cargo build
/cargo run
可以使用现在编译的 crate 再次执行.
Later on, the real source files are copied into the container and cargo build
/ cargo run
can be executed again, using the now-compiled crates.
如果您正在构建一个可执行项目,您还需要在 Cargo.lock 中进行复制.
If you were building an executable project, you'd want to copy in the Cargo.lock as well.
这篇关于Cargo 是否可以在不构建应用程序的情况下下载和构建依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!