Skip to content

Commit 44e5971

Browse files
committed
build(gateway): stop reinstalling the toolchain on every fixture build
The workspace pins a channel plus rustfmt, clippy, rust-analyzer and three targets that the base image does not carry and this build does not need, so the first cargo invocation makes rustup download and install a complete toolchain. That step sat after `COPY . .`, which is the whole repo -- so any change anywhere redid it, in both fixture images, on every push, in all three gateway workflows. It is ~27s per image and it was the bulk of the cost. Measured on an unrelated one-line change: cargo itself reported `Finished in 2.48s`, inside a 29.6s step. Copying the toolchain file onto a layer of its own keys that work on the pin rather than on the source tree. The cargo registry, git and target directories become BuildKit cache mounts, which survive the `COPY . .` invalidation and are shared by both images, so whichever builds second reuses the first's artifacts. The binary is copied out inside the RUN because a cache mount is not part of the resulting layer. Same machine, same kind of change: unrelated file 1m57s -> 4.2s real dependency 45.6s, recompiling six crates The second number is the check that the first is not a false cache hit.
1 parent 35bbd1f commit 44e5971

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

dstack/gateway/test-run/attestation/Dockerfile.mock-attestation

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,41 @@
33

44
FROM rust:1.92-bookworm AS builder
55
WORKDIR /src
6+
7+
# The toolchain file first, on a layer of its own.
8+
#
9+
# The workspace pins a channel plus rustfmt, clippy, rust-analyzer and three
10+
# extra targets, none of which the base image carries and none of which this
11+
# build needs -- so the first cargo invocation makes rustup download and install
12+
# a complete toolchain. Measured at ~27s, in each of the two fixture images, on
13+
# every build: it happened inside the step below, which `COPY . .` invalidates
14+
# on any change anywhere in the repo. Keyed on the toolchain file alone it is a
15+
# cache hit until the pin actually moves.
16+
COPY rust-toolchain.toml ./rust-toolchain.toml
17+
RUN cargo --version
18+
19+
# The cargo caches are BuildKit cache mounts, not image layers, because
20+
# `COPY . .` is the whole repo and busts any layer after it on any source
21+
# change. A mount survives that, and both fixture images name the same three, so
22+
# whichever builds second reuses the first's artifacts. `sharing=locked`
23+
# because cargo does not want two builds in one target directory; they
24+
# serialise rather than corrupt.
25+
#
26+
# The binary is copied out inside the RUN: a cache mount is not part of the
27+
# resulting layer, so a later `COPY --from=builder /src/dstack/target/...`
28+
# would find nothing there.
629
COPY . .
7-
RUN cargo build --manifest-path dstack/Cargo.toml --locked --release \
8-
-p mock-attestation
30+
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
31+
--mount=type=cache,target=/usr/local/cargo/git,sharing=locked \
32+
--mount=type=cache,target=/src/dstack/target,sharing=locked \
33+
cargo build --manifest-path dstack/Cargo.toml --locked --release \
34+
-p mock-attestation \
35+
&& mkdir -p /out && cp dstack/target/release/dstack-mock-attestation /out/
936

1037
FROM debian:bookworm-slim
1138
RUN apt-get update && \
1239
apt-get install -y --no-install-recommends ca-certificates curl && \
1340
rm -rf /var/lib/apt/lists/*
14-
COPY --from=builder /src/dstack/target/release/dstack-mock-attestation /usr/local/bin/
41+
COPY --from=builder /out/dstack-mock-attestation /usr/local/bin/
1542
COPY dstack/gateway/test-run/attestation/tee-simulator.json /etc/dstack/tee-simulator.json
1643
ENTRYPOINT ["/usr/local/bin/dstack-mock-attestation"]

dstack/gateway/test-run/attestation/Dockerfile.simulator

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,43 @@
33

44
FROM rust:1.92-bookworm AS builder
55
WORKDIR /src
6+
7+
# The toolchain file first, on a layer of its own.
8+
#
9+
# The workspace pins a channel plus rustfmt, clippy, rust-analyzer and three
10+
# extra targets, none of which the base image carries and none of which this
11+
# build needs -- so the first cargo invocation makes rustup download and install
12+
# a complete toolchain. Measured at ~27s, in each of the two fixture images, on
13+
# every build: it happened inside the step below, which `COPY . .` invalidates
14+
# on any change anywhere in the repo. Keyed on the toolchain file alone it is a
15+
# cache hit until the pin actually moves.
16+
COPY rust-toolchain.toml ./rust-toolchain.toml
17+
RUN cargo --version
18+
19+
# The cargo caches are BuildKit cache mounts, not image layers, because
20+
# `COPY . .` is the whole repo and busts any layer after it on any source
21+
# change. A mount survives that, and both fixture images name the same three, so
22+
# whichever builds second reuses the first's artifacts. `sharing=locked`
23+
# because cargo does not want two builds in one target directory; they
24+
# serialise rather than corrupt.
25+
#
26+
# The binary is copied out inside the RUN: a cache mount is not part of the
27+
# resulting layer, so a later `COPY --from=builder /src/dstack/target/...`
28+
# would find nothing there.
629
COPY . .
7-
RUN cargo build --manifest-path dstack/Cargo.toml --locked --release \
8-
-p dstack-guest-agent-simulator
30+
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
31+
--mount=type=cache,target=/usr/local/cargo/git,sharing=locked \
32+
--mount=type=cache,target=/src/dstack/target,sharing=locked \
33+
cargo build --manifest-path dstack/Cargo.toml --locked --release \
34+
-p dstack-guest-agent-simulator \
35+
&& mkdir -p /out && cp dstack/target/release/dstack-simulator /out/
936

1037
FROM debian:bookworm-slim
1138
RUN apt-get update && \
1239
apt-get install -y --no-install-recommends ca-certificates && \
1340
rm -rf /var/lib/apt/lists/*
1441
WORKDIR /opt/dstack-simulator
15-
COPY --from=builder /src/dstack/target/release/dstack-simulator /usr/local/bin/dstack-simulator
42+
COPY --from=builder /out/dstack-simulator /usr/local/bin/dstack-simulator
1643
COPY sdk/simulator/app-compose.json sdk/simulator/appkeys.json \
1744
sdk/simulator/sys-config.json sdk/simulator/attestation.bin ./
1845
COPY dstack/gateway/test-run/attestation/simulator.toml ./simulator.toml

0 commit comments

Comments
 (0)