From 4f267225929e43cad00b1b6115084bb97ea70d4a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 18 Aug 2023 11:51:49 +0300 Subject: [PATCH] use cargo-chef to cache build --- Dockerfile | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index b191a47..3701f1b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,25 +1,34 @@ -FROM rust:1-alpine as builder +FROM rust:1-alpine as chef ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL sparse -RUN apk add --no-cache musl-dev +RUN apk add --no-cache musl-dev curl + +# install cargo-binstall +RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh -o install-from-binstall-release.sh +RUN sh install-from-binstall-release.sh + +# install cargo-chef +RUN cargo binstall -y cargo-chef WORKDIR /home/rust/src -COPY Cargo.toml /home/rust/src/ -COPY Cargo.lock /home/rust/src/ -RUN mkdir /home/rust/src/src && \ - echo 'fn main() {}' > /home/rust/src/src/main.rs +FROM chef AS planner -RUN cargo fetch +COPY . . + +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef as builder +COPY --from=planner /home/rust/src/recipe.json recipe.json + +# build & cache deps +RUN cargo chef cook --release --recipe-path recipe.json + +# build app with cached deps +COPY . . RUN cargo build --release -RUN rm -f /home/rust/src/src/main.rs -COPY src /home/rust/src/src -RUN touch /home/rust/src/src/main.rs - -RUN cargo build -v --release - FROM scratch COPY static /static