@@ -18,6 +18,21 @@ RUN --mount=type=cache,id=apt-cache,target=/var/cache/apt,sharing=locked \
1818 build-essential \
1919 musl-dev
2020
21+ # The following block
22+ # creates an empty app, and we copy in Cargo.toml and Cargo.lock as they represent our dependencies
23+ # This allows us to copy in the source in a different layer which in turn allows us to leverage Docker's layer caching
24+ # That means that if our dependencies don't change rebuilding is much faster
25+ WORKDIR /build
26+
27+ RUN cargo init --name ${APPLICATION_NAME}
28+
29+ COPY ./.cargo ./Cargo.toml ./Cargo.lock ./
30+
31+ # We use `fetch` to pre-download the files to the cache
32+ RUN --mount=type=cache,id=cargo-git,target=/usr/local/cargo/git/db,sharing=locked \
33+ --mount=type=cache,id=cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
34+ cargo fetch
35+
2136FROM rust-base AS rust-linux-amd64
2237ARG TARGET=x86_64-unknown-linux-musl
2338
@@ -36,16 +51,6 @@ RUN --mount=type=cache,id=apt-cache-${TARGET},from=rust-base,target=/var/cache/a
3651
3752RUN rustup target add ${TARGET}
3853
39- # The following block
40- # creates an empty app, and we copy in Cargo.toml and Cargo.lock as they represent our dependencies
41- # This allows us to copy in the source in a different layer which in turn allows us to leverage Docker's layer caching
42- # That means that if our dependencies don't change rebuilding is much faster
43- WORKDIR /build
44-
45- RUN cargo init --name ${APPLICATION_NAME}
46-
47- COPY ./.cargo ./Cargo.toml ./Cargo.lock ./
48-
4954RUN --mount=type=cache,target=/build/target/${TARGET},sharing=locked \
5055 --mount=type=cache,id=cargo-git,target=/usr/local/cargo/git/db \
5156 --mount=type=cache,id=cargo-registry,target=/usr/local/cargo/registry \
0 commit comments