Skip to content

Commit d7dd83b

Browse files
committed
opt(orion): reduce image size by optimizing build stages
Signed-off-by: MYUU <1405758738@qq.com>
1 parent 4be5ec5 commit d7dd83b

6 files changed

Lines changed: 69 additions & 158 deletions

File tree

.github/workflows/orion-client-image-deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ jobs:
7070
--target runtime \
7171
--provenance=false \
7272
--sbom=false \
73+
--cache-from "type=gha,scope=orion-client-$ARCH_SUFFIX" \
74+
--cache-to "type=gha,mode=max,scope=orion-client-$ARCH_SUFFIX" \
7375
-t "$IMAGE_BASE:${{ env.IMAGE_TAG_BASE }}-$ARCH_SUFFIX" \
7476
-f orion/Dockerfile \
7577
--push .

orion/Dockerfile

Lines changed: 52 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
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"
1819
ARG RUST_VERSION=1.92-bookworm
19-
ARG RUST_TOOLCHAIN=1.92.0
2020
ARG 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
2828
ARG GIT_COMMIT=unknown
@@ -40,6 +40,7 @@ RUN mkdir -p /build/bin
4040
# Install build dependencies
4141
RUN 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

6191
RUN --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
115150
ARG BUILD_DATE=unknown
116151
ARG 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
131154
RUN 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

186204
COPY --from=builder /build/bin/orion /app/bin/
187205
COPY --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"

orion/docker-compose.yml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# - Scorpio performs real mounts and therefore requires Linux FUSE support.
1212
#
1313
# Profiles:
14-
# dev - Interactive development shell (tooling only)
1514
# fuse - Scorpio daemon (requires privileged mode)
1615
# worker - Orion Worker (requires privileged mode; embeds Scorpio by default)
1716
# =============================================================================
@@ -79,41 +78,12 @@ services:
7978
extra_hosts:
8079
- "host.docker.internal:host-gateway"
8180

82-
# ---------------------------------------------------------------------------
83-
# Development Shell - Tooling Only
84-
# ---------------------------------------------------------------------------
85-
dev:
86-
image: ${ORION_CLIENT_IMAGE:-orion-client:dev}
87-
container_name: mega-dev-shell
88-
profiles:
89-
- dev
90-
privileged: true
91-
environment:
92-
RUST_LOG: ${RUST_LOG:-debug}
93-
volumes:
94-
- ../..:/workspace
95-
- scorpio-data:/data/scorpio
96-
- cargo-cache:/root/.cargo/registry
97-
- target-cache:/workspace/target
98-
working_dir: /workspace
99-
stdin_open: true
100-
tty: true
101-
networks:
102-
- mega-network
103-
extra_hosts:
104-
- "host.docker.internal:host-gateway"
105-
10681
volumes:
10782
scorpio-data:
10883
name: mega-scorpio-data
10984
workspace:
11085
name: mega-workspace
111-
cargo-cache:
112-
name: mega-cargo-cache
113-
target-cache:
114-
name: mega-target-cache
11586

11687
networks:
11788
mega-network:
11889
name: mega-network
119-

orion/entrypoint.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ Examples:
8787
# Run Buck2 build
8888
docker run --rm -v $(pwd):/workspace mega-dev:latest buck2 build //...
8989
90-
# Interactive development
91-
docker run -it --rm -v $(pwd):/workspace mega-dev:dev bash
92-
9390
Environment Variables:
9491
See .env.example for full list of configurable options.
9592

scripts/demo/build-demo-images-local.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ build_and_push() {
262262
fi
263263
local image_tag_with_arch="${image_tag}-${arch_suffix}"
264264
local image_base="${REGISTRY}/${REGISTRY_ALIAS}/${REPOSITORY}"
265+
local cache_dir="${REPO_ROOT}/.buildx-cache/${image_name}-${arch_suffix}"
265266

266267
# Verify paths exist (use absolute paths)
267268
local full_dockerfile="${REPO_ROOT}/${dockerfile_path}"
@@ -358,7 +359,12 @@ build_and_push() {
358359
if [ ${#cache_from_args[@]} -gt 0 ]; then
359360
build_args+=("${cache_from_args[@]}")
360361
fi
361-
362+
363+
# Persist BuildKit cache across builder recreation to speed up local rebuilds.
364+
mkdir -p "${cache_dir}"
365+
build_args+=(--cache-from "type=local,src=${cache_dir}")
366+
build_args+=(--cache-to "type=local,dest=${cache_dir},mode=max")
367+
362368
# Add cache-to (inline cache is always useful)
363369
build_args+=(--cache-to type=inline)
364370

@@ -498,4 +504,4 @@ main() {
498504
}
499505

500506
# Run main function
501-
main "$@"
507+
main "$@"

scripts/dev/build-dev-images-for-local.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ build_image() {
247247
local image_tag_with_arch="${image_tag}-${arch_suffix}"
248248
local image_base="${REPOSITORY}"
249249
local latest_tag="${image_tag%-${GIT_HASH}}-latest"
250+
local cache_dir="${REPO_ROOT}/.buildx-cache/${image_name}-${arch_suffix}"
250251

251252
# Verify paths exist (use absolute paths)
252253
local full_dockerfile="${REPO_ROOT}/${dockerfile_path}"
@@ -283,11 +284,16 @@ build_image() {
283284

284285
# Always load the image into the local Docker engine first.
285286
build_args+=(--load)
286-
287+
287288
if [ "$image_name" = "mega-ui" ]; then
288289
build_args+=(--build-arg APP_ENV=demo)
289290
fi
290291

292+
# Persist BuildKit cache across builder recreation to speed up local rebuilds.
293+
mkdir -p "${cache_dir}"
294+
build_args+=(--cache-from "type=local,src=${cache_dir}")
295+
build_args+=(--cache-to "type=local,dest=${cache_dir},mode=max")
296+
291297
# Add cache-to (inline cache is always useful)
292298
build_args+=(--cache-to type=inline)
293299

0 commit comments

Comments
 (0)