Skip to content

Using cargo-chef increased my build time from 5 minutes to 25 mintues  #273

Open
@kingsleyh

Description

@kingsleyh

Hi

I have a monorepo with a workspace at the root and each micro-service as a cargo project.

when I copy all the files from the root to docker e..g COPY . . and then run the cargo-chef commands as shown in the README - in the chef cook --release it compiles and builds my entire workspace including all the binaries which takes a long time.

Then it does the build release for the binary I actually want:

RUN cargo build --release --bin image-categorisation-service

Is there a way cargo-chef can avoid building the entire workspace when I just want to release the image-categorisation-service binary in a docker image

using cargo-chef has increased the build time for releasing just the image-categorisation-service from 5 minutes to 25 minutes because it's building the entire workspace

ARG RUST_VERSION=1.78.0
ARG APP_NAME=image-categorisation-service

# Install cargo-chef
FROM rust:${RUST_VERSION} AS chef
# We only pay the installation cost once,
# it will be cached from the second build onwards
RUN cargo install cargo-chef
WORKDIR /app

# Prepare the recipe
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

# Build the project
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release --bin image-categorisation-service

# Setup the Runtime
FROM debian:bookworm-slim AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/image-categorisation-service /usr/local/bin
CMD ["/usr/local/bin/image-categorisation-service"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions