百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

cargo-chef

> DevOps
开源

一个 cargo 子命令,可利用 Docker 层缓存来加速 Rust 和 Docker 的构建。

2.7K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个 cargo 子命令,可利用 Docker 层缓存来加速 Rust 和 Docker 的构建。

cargo-chef



> `cargo-chef` was initially developed for the deployment chapter of [Zero to Production In Rust](https://zero2prod.com), a hands-on introduction to backend development using the Rust programming language. # Table of Contents 0. [How to install](#how-to-install) 1. [How to use](#how-to-use) 2. [Benefits vs Limitations](#benefits-vs-limitations) 3. [License](#license) ## How To Install You can install `cargo-chef` from [crates.io](https://crates.io) with ```bash cargo install cargo-chef --locked ``` ## How to use > :warning: **cargo-chef is not meant to be run locally** > Its primary use-case is to speed up container builds by running BEFORE > the actual source code is copied over. Don't run it on existing codebases to avoid > having files being overwritten. `cargo-chef` exposes two commands: `prepare` and `cook`: ```bash cargo chef --help ``` ```text cargo-chef USAGE: cargo chef SUBCOMMANDS: cook Re-hydrate the minimum project skeleton identified by `cargo chef prepare` and build it to cache dependencies prepare Analyze the current project to determine the minimum subset of files (Cargo.lock and Cargo.toml manifests) required to build it and cache dependencies ``` `prepare` examines your project and builds a _recipe_ that captures the set of information required to build your dependencies. ```bash cargo chef prepare --recipe-path recipe.json ``` Nothing too mysterious going on here, you can examine the `recipe.json` file: it contains the skeleton of your project (e.g. all the `Cargo.toml` files with their relative path, the `Cargo.lock` file is available) plus a few additional pieces of information. In particular it makes sure that all libraries and binaries are explicitly declared in their respective `Cargo.toml` files even if they can be found at the canonical default location (`src/main.rs` for a binary, `src/lib.rs` for a library). The `recipe.json` is the equivalent of the Python `requirements.txt` file - it is the only input required for `cargo chef cook`, the command that will build out our dependencies: ```bash cargo chef cook --recipe-path recipe.json ``` If you want to build in `--release` mode: ```bash cargo chef cook --release --recipe-path recipe.json ``` You can also choose to override which Rust toolchain should be used. E.g., to force the `nightly` toolchain: ```bash cargo +nightly chef cook --recipe-path recipe.json ``` `cargo-chef` is designed to be leveraged in Dockerfiles: ``` … ``` We are using three stages: the first computes the recipe file, the second caches our dependencies and builds the binary, the third is our runtime environment. As long as your dependencies do not change the `recipe.json` file will stay the same, therefore the outcome of `cargo chef cook --release --recipe-path recipe.json` will be cached, massively speeding up your builds (up to 5x measured on some commercial projects). ### Pre-built images We offer `lukemathwalker/cargo-chef` as a pre-built Docker image equipped with both Rust and `cargo-chef`. The tagging scheme is `-rust-`. For example, `0.1.74-rust-1.56.0`. We publish tags for every `library/rust` alias, including: - `latest-rust-` (latest `cargo-chef` version, specific Rust version) - `-rust-` (specific `cargo-chef` version, specific Rust version) - `latest` (latest `cargo-chef` version, latest Rust version, matching upstream Rust `latest`) Aliases come directly from [official Rust images](https://hub.docker.com/_/rust/#supported-tags-and-respective-dockerfile-links), e.g.: - `1`, `1.93`, `1.93.1` - `bookworm`, `slim-bookworkm`, `bullseye`, `trixie`, `alpine3.23` - `1.93.1-bookworm`, `1.93.1-slim`, `1.93.1-alpine3.23` Visit [`cargo-chef` page on DockerHub](https://hub.docker.com/r/lukemathwalker/cargo-chef/tags) for an exhaustive list of available tags. > :warning: **You must use the same Rust version in all stages** > If you use a different Rust version in one of the stages > caching will not work as expected. ### Without the pre-built image If you do not want to use the `lukemathwalker/cargo-chef` image, you can simply install the CLI within the Dockerfile: ``` … ``` ### Running the binary in Alpine If you want to run your application using the `alpine` distribution you need to create a fully static binary. The recommended approach is to build for the `x86_64-unknown-linux-musl` target using [`muslrust`](https://github.com/clux/muslrust). `cargo-chef` works for `x86_64-unknown-linux-musl`, but we are **cross-compiling** - the target toolchain must be explicitly specified. A sample Dockerfile looks like this: ``` … ``` ## Benefits vs Limitations `cargo-chef` has been tested on a few OpenSource projects and some of commercial projects, but our testing has definitely not exhausted the range of possibilities when it comes to `cargo build` customisations and we are sure that there are a few rough edges that will have to be smoothed out - please file issues on [GitHub](https://github.com/LukeMathWalker/cargo-chef). ### Benefits of `cargo-chef`: A common alternative is to load a minimal `main.rs` into a container with `Cargo.toml` and `Cargo.lock` to build a Docker layer that consists of only your dependencies ([more info here](https://www.lpalmieri.com/posts/fast-rust-docker-builds/#caching-rust-builds)). This is fragile compared to `cargo-chef` which will instead: - automatically pick up all crates in a workspace (and new ones as they are added) - keep working when files or crates are moved around, which would instead require manual edits to the `Dockerfile` using the "manual" approach - generate fewer intermediate Docker layers (for workspaces) ### Limitations and caveats: - `cargo chef cook` and `cargo build` must be executed from the same working directory. If you examine the `*.d` files under `target/debug/deps` for one of your projects using `cat` you will notice that they contain absolute paths referring to the project `target` directory. If moved around, `cargo` will not leverage them as cached dependencies; - `cargo build` will build local dependencies (outside of the current project) from scratch, even if they are unchanged, due to the reliance of its fingerprinting logic on timestamps (see [this _long_ issue on `cargo`'s repository](https://github.com/rust-lang/cargo/issues/2644)); ## License Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •latest-rust-<alias> (latest cargo-chef version, specific Rust version)
  • •<cargo-chef version>-rust-<alias> (specific cargo-chef version, specific Rust version)
  • •latest (latest cargo-chef version, latest Rust version, matching upstream Rust latest)
  • •1, 1.93, 1.93.1
  • •bookworm, slim-bookworkm, bullseye, trixie, alpine3.23
  • •1.93.1-bookworm, 1.93.1-slim, 1.93.1-alpine3.23
  • •automatically pick up all crates in a workspace (and new ones as they are added)
  • •keep working when files or crates are moved around, which would instead require manual edits to the Dockerfile using the "manual" approach
  • •generate fewer intermediate Docker layers (for workspaces)

> 标签

Rustcargocidockerrust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理