@@ -3,11 +3,11 @@ FROM --platform=${BUILDPLATFORM} rust:1.89.0-trixie@sha256:26318aeddc7e7335b55ab
33
44ARG APPLICATION_NAME
55
6- ENV DEBIAN_FRONTEND=noninteractive
7-
86RUN rm -f /etc/apt/apt.conf.d/docker-clean \
97 && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
108
9+ ARG DEBIAN_FRONTEND=noninteractive
10+
1111# borrowed (Ba Dum Tss!) from
1212# https://github.com/pablodeymo/rust-musl-builder/blob/7a7ea3e909b1ef00c177d9eeac32d8c9d7d6a08c/Dockerfile#L48-L49
1313RUN --mount=type=cache,id=apt-cache,target=/var/cache/apt,sharing=locked \
@@ -18,21 +18,6 @@ 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-
3621FROM rust-base AS rust-linux-amd64
3722ARG TARGET=x86_64-unknown-linux-musl
3823
@@ -41,19 +26,42 @@ ARG TARGET=aarch64-unknown-linux-musl
4126
4227FROM rust-${TARGETPLATFORM//\/ /-} AS rust-cargo-build
4328
44- ENV DEBIAN_FRONTEND=noninteractive
45-
4629COPY ./build-scripts /build-scripts
4730
31+ ARG DEBIAN_FRONTEND=noninteractive
32+
4833RUN --mount=type=cache,id=apt-cache-${TARGET},from=rust-base,target=/var/cache/apt,sharing=locked \
4934 --mount=type=cache,id=apt-lib-${TARGET},from=rust-base,target=/var/lib/apt,sharing=locked \
5035 /build-scripts/setup-env.sh
5136
5237RUN rustup target add ${TARGET}
5338
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+
49+ # We use `fetch` to pre-download the files to the cache
50+ # Notice we do this in the target arch specific branch
51+ # We do this because we want to do it after `setup-env.sh`,
52+ # as the env is less likely to change than the code
53+ # We do lock the cache, to avoid corruption when building it for
54+ # both target platforms. It doesn't matter, as after unlocking the other one
55+ # just validates, but doesn't need to download anything
56+ RUN --mount=type=cache,id=cargo-git,target=/usr/local/cargo/git/db,sharing=locked \
57+ --mount=type=cache,id=cargo-registry-index,target=/usr/local/cargo/registry/index \
58+ --mount=type=cache,id=cargo-registry-cache,target=/usr/local/cargo/registry/cache \
59+ cargo fetch
60+
5461RUN --mount=type=cache,target=/build/target/${TARGET},sharing=locked \
5562 --mount=type=cache,id=cargo-git,target=/usr/local/cargo/git/db \
56- --mount=type=cache,id=cargo-registry,target=/usr/local/cargo/registry \
63+ --mount=type=cache,id=cargo-registry-index,target=/usr/local/cargo/registry/index \
64+ --mount=type=cache,id=cargo-registry-cache,target=/usr/local/cargo/registry/cache \
5765 /build-scripts/build.sh build --release --target ${TARGET} --target-dir ./target/${TARGET}
5866
5967# Rust full build
@@ -70,7 +78,8 @@ RUN touch ./src/main.rs
7078# --release not needed, it is implied with install
7179RUN --mount=type=cache,target=/build/target/${TARGET},sharing=locked \
7280 --mount=type=cache,id=cargo-git,target=/usr/local/cargo/git/db \
73- --mount=type=cache,id=cargo-registry,target=/usr/local/cargo/registry \
81+ --mount=type=cache,id=cargo-registry-index,target=/usr/local/cargo/registry/index \
82+ --mount=type=cache,id=cargo-registry-cache,target=/usr/local/cargo/registry/cache \
7483 /build-scripts/build.sh install --path . --locked --target ${TARGET} --target-dir ./target/${TARGET} --root /output
7584
7685# Container user setup
0 commit comments