# Build stage with cargo-chef for better caching
FROM rust:bookworm AS chef
RUN cargo install cargo-chef
WORKDIR /build
# Plan dependencies
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
# Build dependencies (cached layer)
FROM chef AS builder
COPY --from=planner /build/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release
# Runtime stage
FROM gcr.io/distroless/cc-debian12:nonroot
WORKDIR /home/nonroot
COPY --from=builder /build/target/release/deploy .
COPY --from=builder /build/templates ./templates
ENV PATH=/home/nonroot:$PATH
ENTRYPOINT ["deploy"]