This Dockerfile builds the dependencies in a separate layer with a clean `main` function. This means that subsequent builds (unless the `Cargo.toml` or `Cargo.lock` file change) only rebuild *your* source code. It's substantially faster! ```dockerfile # syntax=docker/dockerfile:1 FROM rust:1.95-bookworm AS builder WORKDIR /app # Copy manifests and lockfile first for better layer caching COPY Cargo.toml Cargo.lock ./ # Create dummy source to pre-build dependencies RUN mkdir src && echo "fn main() {}" > src/main.rs 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 2>/dev/null || true # Copy real source and rebuild (only recompiles the crate itself) COPY src/ src/ RUN --mount=type=cache,target=/usr/local/cargo/registry \ --mount=type=cache,target=/usr/local/cargo/git \ --mount=type=cache,target=/app/target \ touch src/main.rs \ && cargo build --release \ && cp target/release/app /usr/local/bin/app # Runtime stage FROM debian:bookworm-slim RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates \ && rm -rf /var/lib/apt/lists/* COPY --from=builder /usr/local/bin/app /usr/local/bin/ ENTRYPOINT ["app"] ```