@@ -5,7 +5,7 @@ RUN apt-get update \
55 && apt-get install -y ca-certificates wget \
66 && rm -rf /var/lib/apt/lists/*
77
8- FROM --platform=$BUILDPLATFORM rust:1.84 AS prepare
8+ FROM --platform=$BUILDPLATFORM rust:1.91 AS prepare
99
1010RUN apt-get update && apt-get install -y \
1111 gcc-aarch64-linux-gnu \
@@ -22,6 +22,14 @@ ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
2222RUN rustup target add x86_64-unknown-linux-gnu \
2323 aarch64-unknown-linux-gnu
2424
25+ RUN cargo install cargo-chef
26+
27+ FROM prepare AS planner
28+
29+ WORKDIR /build
30+ COPY . .
31+ RUN cargo chef prepare --recipe-path recipe.json
32+
2533FROM prepare AS builder
2634
2735RUN mkdir -p /build
@@ -31,13 +39,22 @@ ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
3139
3240WORKDIR /build
3341
34- COPY . /build
35-
3642RUN touch $RUNTIME_SNAPSHOT_PATH
3743
44+ COPY --from=planner /build/recipe.json recipe.json
45+
46+ # Build dependencies - this is the caching Docker layer!
47+ RUN --mount=type=cache,sharing=locked,target=$CARGO_HOME/git \
48+ --mount=type=cache,sharing=locked,target=$CARGO_HOME/registry \
49+ --mount=type=cache,sharing=locked,target=/build/target \
50+ cargo chef cook --release --recipe-path recipe.json
51+
52+ # Build application
53+ COPY . /build
54+
3855RUN --mount=type=cache,sharing=locked,target=$CARGO_HOME/git \
3956 --mount=type=cache,sharing=locked,target=$CARGO_HOME/registry \
40- --mount=type=cache,sharing=locked,target=/build/openworkers-runner/ target \
57+ --mount=type=cache,sharing=locked,target=/build/target \
4158 cargo run --release --bin snapshot
4259
4360FROM builder AS platform
@@ -46,9 +63,20 @@ ARG TARGETPLATFORM
4663
4764RUN echo "Building for $TARGETPLATFORM"
4865
49- RUN --mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
50- --mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
51- --mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=/build/openworkers-runner/target \
66+ # Build dependencies for target platform
67+ RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
68+ --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
69+ --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
70+ case "$TARGETPLATFORM" in \
71+ "linux/arm64") cargo chef cook --release --target aarch64-unknown-linux-gnu --recipe-path recipe.json ;; \
72+ "linux/amd64") cargo chef cook --release --target x86_64-unknown-linux-gnu --recipe-path recipe.json ;; \
73+ *) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
74+ esac
75+
76+ # Build application for target platform
77+ RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
78+ --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
79+ --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
5280 case "$TARGETPLATFORM" in \
5381 "linux/arm64") cargo build --release --target aarch64-unknown-linux-gnu && \
5482 mv /build/target/aarch64-unknown-linux-gnu/release/openworkers-runner /build/output ;; \
0 commit comments