Skip to content

Commit

Permalink
build: Optimize Dockerfile by adding cache mounts for improved build …
Browse files Browse the repository at this point in the history
…performance

Also copy only the directories that are needed.

Signed-off-by: Eden Reich <[email protected]>
  • Loading branch information
edenreich committed Feb 25, 2025
1 parent e447ca3 commit 22d8196
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ RUN cargo install cargo-chef --locked && \
WORKDIR /app

FROM chef AS planner
COPY . .
COPY Cargo.* ./
COPY src ./src
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS cacher
Expand All @@ -31,13 +32,19 @@ ENV CC=clang \
PATH="/root/.cargo/bin:${PATH}"

COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --target ${TARGET_ARCH} --recipe-path recipe.json
RUN --mount=type=cache,target=/root/.cargo/registry \
--mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/app/target \
cargo chef cook --release --target ${TARGET_ARCH} --recipe-path recipe.json

FROM cacher AS builder
ARG TARGET_ARCH
COPY . .
COPY src ./src
COPY --from=cacher /app/target /app/target
RUN cargo build -vv --release --jobs $(nproc) --target ${TARGET_ARCH}
RUN --mount=type=cache,target=/root/.cargo/registry \
--mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/app/target \
cargo build --release --jobs $(nproc) --target ${TARGET_ARCH}

FROM gcr.io/distroless/static:nonroot AS minimal
ARG TARGET_ARCH
Expand Down

0 comments on commit 22d8196

Please sign in to comment.