You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently cargo chef cook uses default Rust toolchain.
The project can define a Rust toolchain it wants to be built with using rust-toolchain.toml.
This may lead to dependencies being built using different toolchain than the actual project. As a result, cargo build will compile the dependencies again using the toolchain defined in rust-toolchain.toml.
The solution would be to include info about the toolchain into the recipe and use that info in cargo chef cook to make sure it compiles dependencies using the correct toolchain.
Problem reproducer:
cargo new --lib dummy
cd dummy
Create rust-toolchain.toml with following content:
Add tokio as a dependency in Cargo.toml to be able to observe it being compiled twice:
[package]
name = "dummy"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1", features = ["full"] }
Create a Dockerfile:
FROM rust:slim-bullseye AS chef
RUN cargo install cargo-chef
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /recipe.json recipe.json
RUN cargo chef cook --recipe-path recipe.json
COPY . .
RUN cargo build
Run docker build --progress=plain --target builder --no-cache . 2>&1 | tee build-wrong.log
Observe dependencies being compiled both by cargo chef cook and cargo build:
If we copy rust-toolchain.toml into chef image, it will make cargo chef cook to use the same Rust toolchain as the project being compiled:
FROM rust:slim-bullseye AS chef
RUN cargo install cargo-chef
COPY rust-toolchain.toml rust-toolchain.toml # <=== THIS IS ADDED
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /recipe.json recipe.json
RUN cargo chef cook --recipe-path recipe.json
COPY . .
RUN cargo build
Now, when we build it with docker build --progress=plain --target builder --no-cache . 2>&1 | tee build-correct.log we will see only cargo chef cook compiling dependencies and cargo build not doing that.
The only caveat is that you need to be careful in how you define your layers, otherwise you'll end up re-installing the toolchain at each stage. But at least it'll be the right one.
Problem
cargo chef cook
uses default Rust toolchain.rust-toolchain.toml
.cargo build
will compile the dependencies again using the toolchain defined inrust-toolchain.toml
.The solution would be to include info about the toolchain into the recipe and use that info in cargo chef cook to make sure it compiles dependencies using the correct toolchain.
Problem reproducer:
cargo new --lib dummy
rust-toolchain.toml
with following content:tokio
as a dependency inCargo.toml
to be able to observe it being compiled twice:Dockerfile
:docker build --progress=plain --target builder --no-cache . 2>&1 | tee build-wrong.log
cargo chef cook
andcargo build
:Workaround
If we copy
rust-toolchain.toml
intochef
image, it will makecargo chef cook
to use the same Rust toolchain as the project being compiled:Now, when we build it with
docker build --progress=plain --target builder --no-cache . 2>&1 | tee build-correct.log
we will see onlycargo chef cook
compiling dependencies andcargo build
not doing that.Both logs attached
build-correct.log
build-wrong.log
The text was updated successfully, but these errors were encountered: