Skip to content

Commit 1d9918b

Browse files
committed
docker: Build with persistent cache
Cache downloaded and generated files with `RUN --mount=type=cache', so that later builds don't need to download and build everything from scratch. Some Docker versions could run into bugs when a later stage accesses a cached directory from previous stages, so copy the binaries into a separate directory first. Doing so also allows Stage 2 to merge multiple `COPY --from=builder' steps into a single one. Link: https://docs.docker.com/build/cache/optimize/#use-cache-mounts Signed-off-by: Rong Zhang <i@rong.moe>
1 parent 8198f26 commit 1d9918b

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

Dockerfile

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ COPY . .
1717
## This makes the image large but ensures fast startup in Cloud Run
1818
#RUN wget -c https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/clone.bundle -O /usr/src/sashiko/linux-kernel.bundle
1919

20-
# Build for release
21-
RUN cargo build --release
20+
# Build for release with persistent cache
21+
# Note: some Docker versions could run into bugs when a later stage accesses a
22+
# cached directory from previous stages, so copy the binaries into a separate
23+
# directory first.
24+
RUN --mount=type=cache,target=/usr/src/sashiko/target/ \
25+
--mount=type=cache,target=/usr/local/cargo/git/db/ \
26+
--mount=type=cache,target=/usr/local/cargo/registry/ \
27+
cargo build --release && \
28+
mkdir -p /app && \
29+
cd /usr/src/sashiko/target/release/ && \
30+
cp -a sashiko review sashiko-cli /app
2231

2332
# Stage 2: Runtime
2433
FROM debian:bookworm-slim
@@ -36,9 +45,7 @@ RUN apt-get update && apt-get install -y \
3645
WORKDIR /app
3746

3847
# Copy binaries from builder
39-
COPY --from=builder /usr/src/sashiko/target/release/sashiko /usr/local/bin/sashiko
40-
COPY --from=builder /usr/src/sashiko/target/release/review /usr/local/bin/review
41-
COPY --from=builder /usr/src/sashiko/target/release/sashiko-cli /usr/local/bin/sashiko-cli
48+
COPY --from=builder /app /usr/local/bin
4249

4350
## Copy the pre-downloaded kernel bundle
4451
#COPY --from=builder /usr/src/sashiko/linux-kernel.bundle /opt/linux-kernel.bundle

0 commit comments

Comments
 (0)