perf(docker): optimize Rust build times with dependency layer caching
Replace sccache with a two-stage build strategy: dummy source files cache dependency compilation in a separate layer, and --mount=type=cache for Cargo registry/git/target directories. Source-only changes now skip full dependency recompilation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,25 +1,38 @@
|
|||||||
FROM rust:1-bookworm AS builder
|
FROM rust:1-bookworm AS builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install sccache for faster builds
|
# Copy workspace manifests and create dummy source files to cache dependency builds
|
||||||
RUN cargo install sccache --locked
|
|
||||||
ENV RUSTC_WRAPPER=sccache
|
|
||||||
ENV SCCACHE_DIR=/sccache
|
|
||||||
|
|
||||||
COPY Cargo.toml ./
|
COPY Cargo.toml ./
|
||||||
COPY apps/api/Cargo.toml apps/api/Cargo.toml
|
COPY apps/api/Cargo.toml apps/api/Cargo.toml
|
||||||
COPY apps/indexer/Cargo.toml apps/indexer/Cargo.toml
|
COPY apps/indexer/Cargo.toml apps/indexer/Cargo.toml
|
||||||
COPY crates/core/Cargo.toml crates/core/Cargo.toml
|
COPY crates/core/Cargo.toml crates/core/Cargo.toml
|
||||||
COPY crates/parsers/Cargo.toml crates/parsers/Cargo.toml
|
COPY crates/parsers/Cargo.toml crates/parsers/Cargo.toml
|
||||||
|
|
||||||
|
RUN mkdir -p apps/api/src apps/indexer/src crates/core/src crates/parsers/src && \
|
||||||
|
echo "fn main() {}" > apps/api/src/main.rs && \
|
||||||
|
echo "fn main() {}" > apps/indexer/src/main.rs && \
|
||||||
|
echo "" > apps/indexer/src/lib.rs && \
|
||||||
|
echo "" > crates/core/src/lib.rs && \
|
||||||
|
echo "" > crates/parsers/src/lib.rs
|
||||||
|
|
||||||
|
# Build dependencies only (cached as long as Cargo.toml files don't change)
|
||||||
|
RUN --mount=type=cache,target=/usr/local/cargo/registry \
|
||||||
|
--mount=type=cache,target=/usr/local/cargo/git \
|
||||||
|
--mount=type=cache,target=/app/target \
|
||||||
|
cargo build --release -p api && \
|
||||||
|
cargo install sqlx-cli --no-default-features --features postgres --locked
|
||||||
|
|
||||||
|
# Copy real source code and build
|
||||||
COPY apps/api/src apps/api/src
|
COPY apps/api/src apps/api/src
|
||||||
COPY apps/indexer/src apps/indexer/src
|
COPY apps/indexer/src apps/indexer/src
|
||||||
COPY crates/core/src crates/core/src
|
COPY crates/core/src crates/core/src
|
||||||
COPY crates/parsers/src crates/parsers/src
|
COPY crates/parsers/src crates/parsers/src
|
||||||
|
|
||||||
# Build with sccache (cache persisted between builds via Docker cache mount)
|
RUN --mount=type=cache,target=/usr/local/cargo/registry \
|
||||||
RUN --mount=type=cache,target=/sccache \
|
--mount=type=cache,target=/usr/local/cargo/git \
|
||||||
|
--mount=type=cache,target=/app/target \
|
||||||
cargo build --release -p api && \
|
cargo build --release -p api && \
|
||||||
cargo install sqlx-cli --no-default-features --features postgres --locked
|
cp /app/target/release/api /usr/local/bin/api
|
||||||
|
|
||||||
FROM debian:bookworm-slim
|
FROM debian:bookworm-slim
|
||||||
|
|
||||||
@@ -42,7 +55,7 @@ RUN ARCH=$(dpkg --print-architecture) && \
|
|||||||
cp /tmp/lib/libpdfium.so /usr/local/lib/ && \
|
cp /tmp/lib/libpdfium.so /usr/local/lib/ && \
|
||||||
rm -rf /tmp/pdfium.tgz /tmp/lib /tmp/include && \
|
rm -rf /tmp/pdfium.tgz /tmp/lib /tmp/include && \
|
||||||
ldconfig
|
ldconfig
|
||||||
COPY --from=builder /app/target/release/api /usr/local/bin/api
|
COPY --from=builder /usr/local/bin/api /usr/local/bin/api
|
||||||
COPY --from=builder /usr/local/cargo/bin/sqlx /usr/local/bin/sqlx
|
COPY --from=builder /usr/local/cargo/bin/sqlx /usr/local/bin/sqlx
|
||||||
COPY infra/migrations /app/migrations
|
COPY infra/migrations /app/migrations
|
||||||
COPY apps/api/entrypoint.sh /usr/local/bin/entrypoint.sh
|
COPY apps/api/entrypoint.sh /usr/local/bin/entrypoint.sh
|
||||||
|
|||||||
@@ -1,24 +1,37 @@
|
|||||||
FROM rust:1-bookworm AS builder
|
FROM rust:1-bookworm AS builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install sccache for faster builds
|
# Copy workspace manifests and create dummy source files to cache dependency builds
|
||||||
RUN cargo install sccache --locked
|
|
||||||
ENV RUSTC_WRAPPER=sccache
|
|
||||||
ENV SCCACHE_DIR=/sccache
|
|
||||||
|
|
||||||
COPY Cargo.toml ./
|
COPY Cargo.toml ./
|
||||||
COPY apps/api/Cargo.toml apps/api/Cargo.toml
|
COPY apps/api/Cargo.toml apps/api/Cargo.toml
|
||||||
COPY apps/indexer/Cargo.toml apps/indexer/Cargo.toml
|
COPY apps/indexer/Cargo.toml apps/indexer/Cargo.toml
|
||||||
COPY crates/core/Cargo.toml crates/core/Cargo.toml
|
COPY crates/core/Cargo.toml crates/core/Cargo.toml
|
||||||
COPY crates/parsers/Cargo.toml crates/parsers/Cargo.toml
|
COPY crates/parsers/Cargo.toml crates/parsers/Cargo.toml
|
||||||
|
|
||||||
|
RUN mkdir -p apps/api/src apps/indexer/src crates/core/src crates/parsers/src && \
|
||||||
|
echo "fn main() {}" > apps/api/src/main.rs && \
|
||||||
|
echo "fn main() {}" > apps/indexer/src/main.rs && \
|
||||||
|
echo "" > apps/indexer/src/lib.rs && \
|
||||||
|
echo "" > crates/core/src/lib.rs && \
|
||||||
|
echo "" > crates/parsers/src/lib.rs
|
||||||
|
|
||||||
|
# Build dependencies only (cached as long as Cargo.toml files don't change)
|
||||||
|
RUN --mount=type=cache,target=/usr/local/cargo/registry \
|
||||||
|
--mount=type=cache,target=/usr/local/cargo/git \
|
||||||
|
--mount=type=cache,target=/app/target \
|
||||||
|
cargo build --release -p indexer
|
||||||
|
|
||||||
|
# Copy real source code and build
|
||||||
COPY apps/api/src apps/api/src
|
COPY apps/api/src apps/api/src
|
||||||
COPY apps/indexer/src apps/indexer/src
|
COPY apps/indexer/src apps/indexer/src
|
||||||
COPY crates/core/src crates/core/src
|
COPY crates/core/src crates/core/src
|
||||||
COPY crates/parsers/src crates/parsers/src
|
COPY crates/parsers/src crates/parsers/src
|
||||||
|
|
||||||
# Build with sccache (cache persisted between builds via Docker cache mount)
|
RUN --mount=type=cache,target=/usr/local/cargo/registry \
|
||||||
RUN --mount=type=cache,target=/sccache \
|
--mount=type=cache,target=/usr/local/cargo/git \
|
||||||
cargo build --release -p indexer
|
--mount=type=cache,target=/app/target \
|
||||||
|
cargo build --release -p indexer && \
|
||||||
|
cp /app/target/release/indexer /usr/local/bin/indexer
|
||||||
|
|
||||||
FROM debian:bookworm-slim
|
FROM debian:bookworm-slim
|
||||||
|
|
||||||
@@ -39,6 +52,6 @@ RUN ARCH=$(dpkg --print-architecture) && \
|
|||||||
rm -rf /tmp/pdfium.tgz /tmp/lib /tmp/include && \
|
rm -rf /tmp/pdfium.tgz /tmp/lib /tmp/include && \
|
||||||
ldconfig
|
ldconfig
|
||||||
|
|
||||||
COPY --from=builder /app/target/release/indexer /usr/local/bin/indexer
|
COPY --from=builder /usr/local/bin/indexer /usr/local/bin/indexer
|
||||||
EXPOSE 7081
|
EXPOSE 7081
|
||||||
CMD ["/usr/local/bin/indexer"]
|
CMD ["/usr/local/bin/indexer"]
|
||||||
|
|||||||
Reference in New Issue
Block a user