1- # Shared workspace builder stage - builds all dependencies once
2- FROM rust:1-slim-trixie AS builder
3-
1+ # === DEPENDENCY PLANNING ===
2+ # Use pre-built cargo-chef image so we don't install it every build
3+ FROM lukemathwalker/cargo-chef:latest-rust-slim-trixie AS chef
44WORKDIR /app
55
6- # Install build dependencies
6+ # Analyze the workspace and produce a recipe file describing all dependencies
7+ FROM chef AS planner
8+ COPY . .
9+ RUN cargo chef prepare --recipe-path recipe.json
10+
11+ # === DEPENDENCY BUILDING ===
12+ # Build ONLY third-party dependencies from the recipe. This layer is cached
13+ # until any Cargo.toml changes — source code changes don't affect it.
14+ FROM chef AS cacher
715RUN apt-get update && \
816 apt-get install -y --no-install-recommends pkg-config libssl-dev && \
917 rm -rf /var/lib/apt/lists/*
1018
11- # Copy workspace Cargo.toml
12- COPY Cargo.toml ./
13-
14- # Copy all member Cargo.toml files for workspace resolution
15- COPY packages/shared/Cargo.toml packages/shared/
16- COPY packages/api/Cargo.toml packages/api/
17- COPY packages/scanner/Cargo.toml packages/scanner/
18- COPY migration/Cargo.toml migration/
19+ COPY --from=planner /app/recipe.json recipe.json
20+ RUN cargo chef cook --workspace --release --recipe-path recipe.json
1921
20- # Create stub files for dependency resolution
21- RUN mkdir -p packages/shared/src packages/api/src packages/scanner/src migration/src && \
22- echo "pub fn main() {}" > packages/shared/src/lib.rs && \
23- echo "fn main() {}" > packages/api/src/main.rs && \
24- echo "fn main() {}" > packages/scanner/src/main.rs && \
25- echo "pub fn main() {}" > migration/src/lib.rs
22+ # === APPLICATION BUILD ===
23+ # Copy compiled deps from cacher, then copy source and build. Only your
24+ # actual .rs files get recompiled when they change.
25+ FROM chef AS builder
26+ RUN apt-get update && \
27+ apt-get install -y --no-install-recommends pkg-config libssl-dev && \
28+ rm -rf /var/lib/apt/lists/*
2629
27- # Fetch dependencies (cached unless Cargo.toml files change)
28- RUN cargo fetch
30+ # Bring in pre-compiled dependency artifacts
31+ COPY --from=cacher /app/target target
32+ COPY --from=cacher /usr/local/cargo /usr/local/cargo
2933
30- # Copy actual source code
31- COPY packages/shared/src packages/shared/src
32- COPY packages/api/src packages/api/src
33- COPY packages/scanner/src packages/scanner/src
34- COPY migration/src migration/src
34+ # Copy everything (source code + Cargo.toml files)
35+ COPY . .
3536
36- # Build entire workspace - all shared dependencies compiled once
37+ # Build workspace — deps already compiled, only app code compiles
3738RUN cargo build --workspace --release
3839
39- # API service runtime
40+ # === API RUNTIME ===
4041FROM debian:trixie-slim AS api-runtime
4142
4243RUN apt-get update && \
@@ -58,7 +59,7 @@ ENV LISTEN_ADDR=0.0.0.0:3000
5859
5960CMD ["nmcscan-api" ]
6061
61- # Scanner service runtime
62+ # === SCANNER RUNTIME ===
6263FROM debian:trixie-slim AS scanner-runtime
6364
6465RUN apt-get update && \
0 commit comments