1- # ---------- planner stage ----------
2- # This stage analyzes the Cargo workspace and produces a dependency recipe.
3- # The recipe will change ONLY when Cargo.toml changes.
1+ # ────── Stage 0: Chef ──────
2+ # Downloads and installs cargo-chef (prebuilt binary, no Rust compilation needed).
43FROM rust:1.95.0 AS chef
54
65ARG TARGETARCH=amd64
@@ -11,7 +10,6 @@ WORKDIR /opt/mega
1110# Prebuilt cargo-chef (avoids compiling it on every cold planner layer).
1211RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl \
1312 && rm -rf /var/lib/apt/lists/* \
14- && set -eux \
1513 && case "$TARGETARCH" in \
1614 amd64) chef_arch=x86_64-unknown-linux-musl ;; \
1715 arm64) chef_arch=aarch64-unknown-linux-gnu ;; \
@@ -20,6 +18,13 @@ RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates
2018 && curl -fsSL "https://github.com/LukeMathWalker/cargo-chef/releases/download/v${CARGO_CHEF_VERSION}/cargo-chef-${chef_arch}.tar.gz" \
2119 | tar xz -C /usr/local/cargo/bin
2220
21+ # ────── Stage 1: Planner ──────
22+ # Copies all Cargo.toml files; cargo-chef emits recipe.json describing the full dependency graph.
23+ # This stage is cached unless a Cargo.toml changes.
24+ FROM chef AS planner
25+
26+ WORKDIR /opt/mega
27+
2328COPY Cargo.toml ./
2429COPY api-model/Cargo.toml api-model/
2530COPY ceres/Cargo.toml ceres/
@@ -38,14 +43,13 @@ COPY orion-server/bellatrix/Cargo.toml orion-server/bellatrix/
3843COPY saturn/Cargo.toml saturn/
3944COPY vault/Cargo.toml vault/
4045
46+ # Named cache mounts let the builder stage inherit this cache via `from=planner`.
47+ RUN --mount=type=cache,target=/usr/local/cargo/registry,id=mono-registry \
48+ --mount=type=cache,target=/usr/local/cargo/git,id=mono-git \
49+ cargo chef prepare --bin mono --recipe-path recipe.json
4150
42- # Generate a recipe describing the dependency graph
43- RUN cargo chef prepare --bin mono --recipe-path recipe.json
44-
45-
46- # ---------- builder stage ----------
47- # This stage builds dependencies first (cached),
48- # then builds the actual application binary.
51+ # ────── Stage 2: Builder ──────
52+ # Installs system libs, builds all dependencies (cached), then builds the mono binary.
4953FROM chef AS builder
5054
5155WORKDIR /opt/mega
@@ -60,56 +64,34 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
6064 libprotobuf-dev \
6165 && rm -rf /var/lib/apt/lists/*
6266
63- # Faster linking ( release deps + final mono link) .
67+ # Faster linking for release builds .
6468ENV RUSTFLAGS="-C link-arg=-fuse-ld=mold"
6569
66- # Copy the dependency recipe from the planner stage
67- COPY --from=chef /opt/mega/recipe.json recipe.json
70+ # Inherit registry/git caches from the planner stage.
71+ COPY --from=planner /opt/mega/recipe.json recipe.json
6872
69- # Build and cache all Rust dependencies
70- # This layer will be reused as long as dependencies do not change
71- RUN --mount=type=cache,target=/usr/local/cargo/registry \
72- --mount=type=cache,target=/usr/local/cargo/git \
73- cargo chef cook --bin mono --release --recipe-path recipe.json
73+ RUN --mount=type=cache,target=/usr/local/cargo/registry,id=mono-registry,from=planner \
74+ --mount=type=cache,target=/usr/local/cargo/git,id=mono-git,from=planner \
75+ cargo chef cook --release --recipe-path recipe.json
7476
75- # Copy only the workspace sources AFTER dependencies are cached.
76- # This avoids sending the entire repo context (which can be multiple GB) to the builder.
77- COPY Cargo.toml ./
78- COPY api-model ./api-model
79- COPY ceres ./ceres
80- COPY common ./common
81- COPY config ./config
82- COPY context ./context
83- COPY io-orbit ./io-orbit
84- COPY jupiter ./jupiter
85- COPY mono ./mono
86- COPY orion ./orion
87- COPY orion-server ./orion-server
88- COPY saturn ./saturn
89- COPY vault ./vault
90-
91- # Build the mono binary (fixed release)
92- #
93- # NOTE: Build output must be persisted into the image layer for the runtime stage `COPY`.
94- # A buildkit cache mount is not committed into the layer, so we build into a cached dir
95- # then copy the final binary into `/opt/mega/target/...` (regular filesystem).
96- RUN --mount=type=cache,target=/usr/local/cargo/registry \
97- --mount=type=cache,target=/usr/local/cargo/git \
77+ COPY . .
78+
79+ # Persist built binary into the image layer (buildkit cache mounts evaporate after build).
80+ RUN --mount=type=cache,target=/usr/local/cargo/registry,id=mono-registry,from=planner \
81+ --mount=type=cache,target=/usr/local/cargo/git,id=mono-git,from=planner \
9882 --mount=type=cache,target=/opt/mega/target-cache \
9983 CARGO_TARGET_DIR=/opt/mega/target-cache cargo build --release -p mono && \
10084 mkdir -p /opt/mega/target/release && \
10185 cp /opt/mega/target-cache/release/mono /opt/mega/target/release/mono
10286
103-
104- # ---------- runtime stage ----------
105- # This is the minimal runtime image containing only the binary and runtime deps
106- FROM debian:bookworm-slim
87+ # ────── Stage 3: Runtime ──────
88+ FROM debian:trixie-slim
10789
10890# Install runtime dependencies
10991RUN apt-get update && apt-get install -y --no-install-recommends \
11092 ca-certificates \
11193 less \
112- libssl3 \
94+ libssl3t64 \
11395 && rm -rf /var/lib/apt/lists/*
11496
11597# Copy the compiled binary and startup script
0 commit comments