feat(docker): optimize build caching and fix binary path

- Copy Cargo files first to leverage Docker layer caching
- Build dependencies separately before copying source code
- Use cache mounts for cargo registry to speed up builds
- Fix binary path in final stage from cargo bin to target release
This commit is contained in:
2025-11-21 07:33:42 +01:00
parent 4ee1ef0a81
commit 4482c4041e
+17 -5
View File
@@ -4,12 +4,24 @@ FROM rust:1.91 AS builder
WORKDIR /usr/src/pop_imap_importer
# Copy manifests first to leverage Docker layer caching
COPY src/ Cargo.* ./
COPY Cargo.* ./
# Create a dummy src/main.rs file to build dependencies
RUN mkdir -p src && \
echo "fn main() {}" > src/main.rs
# Build dependencies only - this will be cached as long as Cargo.toml/Cargo.lock don't change
RUN --mount=type=cache,target=/usr/local/cargo/registry \
cargo build --release
# Copy the actual source code
COPY src src
RUN touch src/main.rs
# Build the actual application
RUN cargo build --release
RUN cargo install --path .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
ls -lh target/release && \
cargo build --release
# Runtime stage
FROM debian:bookworm-slim
@@ -20,7 +32,7 @@ RUN apt-get update && \
rm -rf /var/lib/apt/lists/*
# RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/pop_imap_importer /usr/local/bin/pop_imap_importer
COPY --from=builder /usr/src/pop_imap_importer/target/release/rs_pop_imap_importer /usr/local/bin/pop_imap_importer
CMD ["pop_imap_importer"]