Image builds keep compiled crates between builds (#438)

The ipx Dockerfile built its dependencies in a layer of stub crates,
which any change to Cargo.toml or Cargo.lock invalidated: all of them
compiled again in release, 405 crates, 170 s at the builder's cap. The
directory's had no such layer and compiled everything every time. Both
now build with cargo's registry and target/ as BuildKit cache mounts.
Measured on Tower: a Cargo.toml change rebuilds three crates in 52 s
instead of 405 in 170 s. The peak is about the same, since the ipx
crate itself is most of it; what goes is the minutes at the cap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 14:05:12 +00:00
parent f9e4c1d800
commit 3a3900e959
3 changed files with 24 additions and 21 deletions

View File

@@ -7,25 +7,22 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /src
# Dependencies first, so editing the source does not rebuild librqbit every time: each crate of
# the workspace as an empty stub, its manifest real.
COPY Cargo.toml Cargo.lock ./
COPY crates/ipx/Cargo.toml crates/ipx/
COPY crates/feed/Cargo.toml crates/feed/
COPY crates/directory/Cargo.toml crates/directory/
COPY crates/tui/Cargo.toml crates/tui/
RUN mkdir -p crates/ipx/src crates/feed/src crates/directory/src crates/tui/src \
&& echo 'fn main(){}' > crates/ipx/src/main.rs && touch crates/feed/src/lib.rs \
&& echo 'fn main(){}' > crates/directory/src/main.rs && touch crates/directory/src/lib.rs \
&& echo 'fn main(){}' > crates/tui/src/main.rs \
&& cargo build --release --locked -p ipx \
&& rm -rf crates/ipx/src crates/feed/src crates/directory/src crates/tui/src
COPY crates ./crates
# ipx builds the directory crate in, whose public pages serve these (#215).
COPY web/logo.svg web/logo-dark.svg web/favicon.png ./web/
# cargo skips a rebuild if mtimes look untouched; make sure it does not.
RUN touch crates/ipx/src/main.rs crates/ipx/src/lib.rs crates/feed/src/lib.rs && cargo build --release --locked -p ipx
# cargo's registry and target/ are BuildKit cache mounts, kept in the builder between builds, so
# a Cargo.lock change compiles only the crates that changed (#438): a dependency layer of stub
# crates went with any change to it and compiled all 500 again, holding the builder's memory cap
# for minutes. A cache mount is not part of the image, so the binary is copied out in the same
# step. The workspace's own crates are touched because cargo goes by mtimes and skips a rebuild
# if they look untouched.
RUN --mount=type=cache,id=ipx-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
--mount=type=cache,id=ipx-target,target=/src/target,sharing=locked \
touch crates/ipx/src/main.rs crates/ipx/src/lib.rs crates/feed/src/lib.rs \
crates/directory/src/lib.rs \
&& cargo build --release --locked -p ipx \
&& cp target/release/ipxd /usr/local/bin/ipxd
FROM debian:trixie-slim
# ffmpeg is DeadAir's (#171): it finds the silences and copies the frames kept. Debian's adds
@@ -33,7 +30,7 @@ FROM debian:trixie-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates gosu ffmpeg \
&& rm -rf /var/lib/apt/lists/*
COPY --from=build /src/target/release/ipxd /usr/local/bin/ipxd
COPY --from=build /usr/local/bin/ipxd /usr/local/bin/ipxd
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh