77# - Buck2 (build tool)
88#
99# Supports: linux/amd64, linux/arm64
10- # Build targets: runtime (slim), dev (with Rust toolchain)
11- # Optional build targets: runtime-nocache, dev-nocache (disable BuildKit cargo cache mounts)
10+ # Final stage: runtime (slim, default)
11+ # NOTE: This Dockerfile assumes the Docker build context is the repo root.
12+ # Example: `docker build -f orion/Dockerfile .`
1213# =============================================================================
1314
1415# -----------------------------------------------------------------------------
1516# Build Arguments
1617# -----------------------------------------------------------------------------
1718# Use "bookworm" for latest stable, or specify version like "1.83"
1819ARG RUST_VERSION=1.92-bookworm
19- ARG RUST_TOOLCHAIN=1.92.0
2020ARG BUCK2_VERSION=2025-06-01
2121
2222# =============================================================================
23- # Stage 1: Builder Base - Prepare workspace
23+ # Stage 1: Chef Base - Tooling and system dependencies
2424# =============================================================================
25- FROM rust:${RUST_VERSION} AS builder -base
25+ FROM rust:${RUST_VERSION} AS chef -base
2626
2727# Build arguments for version tracking
2828ARG GIT_COMMIT=unknown
@@ -40,6 +40,7 @@ RUN mkdir -p /build/bin
4040# Install build dependencies
4141RUN apt-get update && apt-get install -y --no-install-recommends \
4242 build-essential \
43+ binutils \
4344 pkg-config \
4445 cmake \
4546 clang \
@@ -50,27 +51,61 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
5051 protobuf-compiler \
5152 && rm -rf /var/lib/apt/lists/*
5253
53- # Copy entire project
54- COPY . .
54+ # Install cargo-chef for dependency layer caching
55+ RUN cargo install cargo-chef --version 0.1.73
56+
57+ # =============================================================================
58+ # Stage 1a: Planner - Generate dependency recipe
59+ # =============================================================================
60+ FROM chef-base AS planner
61+
62+ COPY Cargo.toml ./
63+ COPY Cargo.lock ./
64+ COPY api-model/Cargo.toml api-model/
65+ COPY ceres/Cargo.toml ceres/
66+ COPY common/Cargo.toml common/
67+ COPY context/Cargo.toml context/
68+ COPY io-orbit/Cargo.toml io-orbit/
69+ COPY jupiter/Cargo.toml jupiter/
70+ COPY jupiter/callisto/Cargo.toml jupiter/callisto/
71+ COPY mono/Cargo.toml mono/
72+ COPY orion/Cargo.toml orion/
73+ COPY orion/audit/Cargo.toml orion/audit/
74+ COPY orion/td_util/Cargo.toml orion/td_util/
75+ COPY orion/buck/Cargo.toml orion/buck/
76+ COPY orion-server/Cargo.toml orion-server/
77+ COPY orion-server/bellatrix/Cargo.toml orion-server/bellatrix/
78+ COPY saturn/Cargo.toml saturn/
79+ COPY scorpio/Cargo.toml scorpio/
80+ COPY vault/Cargo.toml vault/
81+
82+ RUN cargo chef prepare --recipe-path recipe.json
5583
5684# =============================================================================
57- # Stage 1a : Builder (BuildKit cache enabled) - Compile Rust binaries
85+ # Stage 1b : Builder - Build cached deps, then compile binaries
5886# =============================================================================
59- FROM builder-base AS builder
87+ FROM chef-base AS builder
88+
89+ COPY --from=planner /build/recipe.json /build/recipe.json
6090
6191RUN --mount=type=cache,id=mega-cargo-registry-${TARGETARCH},target=/usr/local/cargo/registry,sharing=locked \
6292 --mount=type=cache,id=mega-cargo-git-${TARGETARCH},target=/usr/local/cargo/git,sharing=locked \
6393 --mount=type=cache,id=mega-cargo-target-${TARGETARCH},target=/build/target,sharing=locked \
64- CARGO_TARGET_DIR=/build/target cargo build --release --package orion --package scorpio \
65- && install -m 0755 /build/target/release/orion /build/bin/orion \
66- && install -m 0755 /build/target/release/scorpio /build/bin/scorpio
94+ CARGO_TARGET_DIR=/build/target \
95+ CARGO_INCREMENTAL=0 \
96+ CARGO_PROFILE_RELEASE_DEBUG=0 \
97+ cargo chef cook --release --recipe-path recipe.json
6798
68- # =============================================================================
69- # Stage 1b: Builder (no BuildKit cache) - Compile Rust binaries
70- # =============================================================================
71- FROM builder-base AS builder-nocache
99+ COPY . .
72100
73- RUN CARGO_TARGET_DIR=/build/target cargo build --release --package orion --package scorpio \
101+ RUN --mount=type=cache,id=mega-cargo-registry-${TARGETARCH},target=/usr/local/cargo/registry,sharing=locked \
102+ --mount=type=cache,id=mega-cargo-git-${TARGETARCH},target=/usr/local/cargo/git,sharing=locked \
103+ --mount=type=cache,id=mega-cargo-target-${TARGETARCH},target=/build/target,sharing=locked \
104+ CARGO_TARGET_DIR=/build/target \
105+ CARGO_INCREMENTAL=0 \
106+ CARGO_PROFILE_RELEASE_DEBUG=0 \
107+ cargo build --release --package orion --package scorpio \
108+ && strip /build/target/release/orion /build/target/release/scorpio \
74109 && install -m 0755 /build/target/release/orion /build/bin/orion \
75110 && install -m 0755 /build/target/release/scorpio /build/bin/scorpio
76111
@@ -115,30 +150,13 @@ ARG GIT_COMMIT=unknown
115150ARG BUILD_DATE=unknown
116151ARG BUCK2_VERSION
117152
118- # Image metadata
119- LABEL org.opencontainers.image.title="Mega Dev Image" \
120- org.opencontainers.image.description="Unified development image with Orion Worker, Scorpio, and Buck2" \
121- org.opencontainers.image.version="1.0.0" \
122- org.opencontainers.image.revision="${GIT_COMMIT}" \
123- org.opencontainers.image.created="${BUILD_DATE}" \
124- org.opencontainers.image.source="https://github.com/web3infra-foundation/mega" \
125- mega.orion.version="${GIT_COMMIT}" \
126- mega.orion_worker.version="${GIT_COMMIT}" \
127- mega.scorpio.version="${GIT_COMMIT}" \
128- mega.buck2.version="${BUCK2_VERSION}"
129-
130153# Install runtime dependencies
131154RUN apt-get update && apt-get install -y --no-install-recommends \
132155 ca-certificates \
133156 fuse3 \
134- libfuse3-3 \
135157 libssl3 \
136- git \
137- git-lfs \
138- curl \
139158 gettext-base \
140159 netcat-openbsd \
141- procps \
142160 && rm -rf /var/lib/apt/lists/* \
143161 # Configure FUSE to allow other users
144162 && echo "user_allow_other" >> /etc/fuse.conf
@@ -185,91 +203,3 @@ FROM runtime-base AS runtime
185203
186204COPY --from=builder /build/bin/orion /app/bin/
187205COPY --from=builder /build/bin/scorpio /app/bin/
188-
189- # =============================================================================
190- # Stage 3b: Runtime (no BuildKit cache) - Copy binaries built without cache
191- # =============================================================================
192- FROM runtime-base AS runtime-nocache
193-
194- COPY --from=builder-nocache /build/bin/orion /app/bin/
195- COPY --from=builder-nocache /build/bin/scorpio /app/bin/
196-
197- # =============================================================================
198- # Stage 4: Dev - Full development image with Rust toolchain
199- # =============================================================================
200- FROM runtime AS dev
201-
202- # Keep dev toolchain pinned (must be re-declared after FROM).
203- ARG RUST_TOOLCHAIN
204-
205- # Install development tools
206- RUN apt-get update && apt-get install -y --no-install-recommends \
207- build-essential \
208- pkg-config \
209- cmake \
210- clang \
211- llvm-dev \
212- libclang-dev \
213- libssl-dev \
214- libfuse3-dev \
215- vim \
216- less \
217- jq \
218- htop \
219- && rm -rf /var/lib/apt/lists/*
220-
221- # Install Rust toolchain
222- RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | \
223- sh -s -- -y --default-toolchain ${RUST_TOOLCHAIN} \
224- && . $HOME/.cargo/env \
225- && rustup component add rustfmt clippy rust-analyzer
226-
227- # Set Rust environment
228- ENV PATH="/root/.cargo/bin:${PATH}" \
229- CARGO_HOME="/root/.cargo" \
230- RUSTUP_HOME="/root/.rustup"
231-
232- # Install additional Cargo tools
233- RUN cargo install cargo-watch cargo-expand
234-
235- LABEL mega.image.type="dev"
236-
237- # =============================================================================
238- # Stage 4b: Dev (no BuildKit cache) - Full development image with Rust toolchain
239- # =============================================================================
240- FROM runtime-nocache AS dev-nocache
241-
242- # Keep dev toolchain pinned (must be re-declared after FROM).
243- ARG RUST_TOOLCHAIN
244-
245- # Install development tools
246- RUN apt-get update && apt-get install -y --no-install-recommends \
247- build-essential \
248- pkg-config \
249- cmake \
250- clang \
251- llvm-dev \
252- libclang-dev \
253- libssl-dev \
254- libfuse3-dev \
255- vim \
256- less \
257- jq \
258- htop \
259- && rm -rf /var/lib/apt/lists/*
260-
261- # Install Rust toolchain
262- RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | \
263- sh -s -- -y --default-toolchain ${RUST_TOOLCHAIN} \
264- && . $HOME/.cargo/env \
265- && rustup component add rustfmt clippy rust-analyzer
266-
267- # Set Rust environment
268- ENV PATH="/root/.cargo/bin:${PATH}" \
269- CARGO_HOME="/root/.cargo" \
270- RUSTUP_HOME="/root/.rustup"
271-
272- # Install additional Cargo tools
273- RUN cargo install cargo-watch cargo-expand
274-
275- LABEL mega.image.type="dev"
0 commit comments