opt(orion): reduce image size by optimizing build stages#1954
Conversation
|
Claude finished @slowslow32's task in 2m 39s —— View job Code ReviewPR: opt(orion): reduce image size by optimizing build stages
SummaryThis PR significantly reduces the orion-client image from ~1GB to 321MB through a well-structured multi-stage Dockerfile rewrite. The optimization approach is sound and the code quality is generally high. A few issues are worth addressing. Dockerfile (
|
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
There was a problem hiding this comment.
Pull request overview
This PR reduces the Orion client + Scorpio container image size by simplifying the Docker build to a slim runtime-only target and improving build caching.
Changes:
- Refactors
orion/Dockerfileto usecargo-chef, strip binaries, and remove dev/nocache targets and extra runtime packages. - Adds persistent BuildKit cache exports for local build scripts, and GHA cache for the image deploy workflow.
- Removes the docker-compose “dev shell” profile/service and related docs snippets.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| scripts/dev/build-dev-images-for-local.sh | Persists local BuildKit cache between rebuilds via type=local cache. |
| scripts/demo/build-demo-images-local.sh | Same local BuildKit cache persistence for demo image builds. |
| orion/entrypoint.sh | Removes interactive dev example from help text. |
| orion/docker-compose.yml | Drops dev profile/service and its cache volumes. |
| orion/Dockerfile | Reworks build stages for smaller runtime image (cargo-chef + strip; runtime-only target). |
| .github/workflows/orion-client-image-deploy.yml | Adds type=gha BuildKit caching to speed up CI builds. |
| COPY Cargo.toml ./ | ||
| COPY Cargo.lock ./ | ||
| COPY api-model/Cargo.toml api-model/ | ||
| COPY ceres/Cargo.toml ceres/ | ||
| COPY common/Cargo.toml common/ | ||
| COPY context/Cargo.toml context/ | ||
| COPY extensions/rag/chat/Cargo.toml extensions/rag/chat/ | ||
| COPY extensions/rag/index/Cargo.toml extensions/rag/index/ | ||
| COPY extensions/observatory/Cargo.toml extensions/observatory/ | ||
| COPY io-orbit/Cargo.toml io-orbit/ | ||
| COPY jupiter/Cargo.toml jupiter/ | ||
| COPY jupiter/callisto/Cargo.toml jupiter/callisto/ | ||
| COPY mono/Cargo.toml mono/ | ||
| COPY orion/Cargo.toml orion/ | ||
| COPY orion/audit/Cargo.toml orion/audit/ | ||
| COPY orion/td_util/Cargo.toml orion/td_util/ | ||
| COPY orion/buck/Cargo.toml orion/buck/ | ||
| COPY orion-server/Cargo.toml orion-server/ | ||
| COPY orion-server/bellatrix/Cargo.toml orion-server/bellatrix/ | ||
| COPY saturn/Cargo.toml saturn/ | ||
| COPY scorpio/Cargo.toml scorpio/ | ||
| COPY vault/Cargo.toml vault/ | ||
|
|
There was a problem hiding this comment.
The planner stage manually enumerates many COPY <crate>/Cargo.toml ... entries. This is easy to get out of sync with workspace members and path dependencies, which will break cargo chef prepare during image builds. Consider adding a brief comment here calling out that new crates/workspace members must be added to this list, or switching to a pattern that copies manifests via .dockerignore to reduce maintenance overhead.
| local image_tag_with_arch="${image_tag}-${arch_suffix}" | ||
| local image_base="${REPOSITORY}" | ||
| local latest_tag="${image_tag%-${GIT_HASH}}-latest" | ||
| local cache_dir="${REPO_ROOT}/.buildx-cache/${image_name}-${arch_suffix}" |
There was a problem hiding this comment.
This introduces a persistent BuildKit cache under ${REPO_ROOT}/.buildx-cache/..., but the repo's .gitignore currently doesn't ignore .buildx-cache. That will show up as untracked files and can be accidentally committed; consider either writing caches under a user cache dir (e.g. $XDG_CACHE_HOME/~/.cache) or add .buildx-cache/ to .gitignore.
| local cache_dir="${REPO_ROOT}/.buildx-cache/${image_name}-${arch_suffix}" | |
| # Use a user cache directory for BuildKit cache to avoid polluting the repo. | |
| # Prefer XDG_CACHE_HOME, then HOME-based cache, then /tmp as a last resort. | |
| local cache_root | |
| if [ -n "${XDG_CACHE_HOME:-}" ]; then | |
| cache_root="${XDG_CACHE_HOME}" | |
| elif [ -n "${HOME:-}" ]; then | |
| cache_root="${HOME}/.cache" | |
| else | |
| cache_root="/tmp/.cache" | |
| fi | |
| local cache_dir="${cache_root}/mega-buildx/${image_name}-${arch_suffix}" |
| mkdir -p "${cache_dir}" | ||
| build_args+=(--cache-from "type=local,src=${cache_dir}") | ||
| build_args+=(--cache-to "type=local,dest=${cache_dir},mode=max") |
There was a problem hiding this comment.
--cache-to type=local,...,mode=max can grow very large over time and is always enabled now. Consider making this opt-in (env flag), or at least printing the cache path + adding a simple clean target (e.g. --no-cache / --clean-cache) so local disk usage doesn't become a surprise.
| mkdir -p "${cache_dir}" | |
| build_args+=(--cache-from "type=local,src=${cache_dir}") | |
| build_args+=(--cache-to "type=local,dest=${cache_dir},mode=max") | |
| # This can grow large over time, so it can be disabled via NO_LOCAL_CACHE=1. | |
| if [ -z "${NO_LOCAL_CACHE:-}" ]; then | |
| mkdir -p "${cache_dir}" | |
| log_info "Using local BuildKit cache at: ${cache_dir}" | |
| build_args+=(--cache-from "type=local,src=${cache_dir}") | |
| build_args+=(--cache-to "type=local,dest=${cache_dir},mode=max") | |
| else | |
| log_info "Local BuildKit cache (type=local,mode=max) disabled via NO_LOCAL_CACHE" | |
| fi |
| fi | ||
| local image_tag_with_arch="${image_tag}-${arch_suffix}" | ||
| local image_base="${REGISTRY}/${REGISTRY_ALIAS}/${REPOSITORY}" | ||
| local cache_dir="${REPO_ROOT}/.buildx-cache/${image_name}-${arch_suffix}" |
There was a problem hiding this comment.
Same as in the dev build script: this creates ${REPO_ROOT}/.buildx-cache/... but .gitignore doesn't ignore .buildx-cache. Consider moving the cache outside the repo or adding it to .gitignore to avoid noisy git status / accidental commits.
| local cache_dir="${REPO_ROOT}/.buildx-cache/${image_name}-${arch_suffix}" | |
| # Use a per-user cache directory outside the repo to avoid polluting git status. | |
| local cache_root="${XDG_CACHE_HOME:-${HOME}/.cache}/mega/buildx" | |
| local cache_dir="${cache_root}/${image_name}-${arch_suffix}" |
| # Persist BuildKit cache across builder recreation to speed up local rebuilds. | ||
| mkdir -p "${cache_dir}" | ||
| build_args+=(--cache-from "type=local,src=${cache_dir}") | ||
| build_args+=(--cache-to "type=local,dest=${cache_dir},mode=max") | ||
|
|
There was a problem hiding this comment.
The local BuildKit cache export (mode=max) is always enabled now and can grow significantly on developer machines. Consider gating this behind an env var / flag or documenting a cleanup path to prevent unexpected disk usage.
| # Copy entire project | ||
| COPY . . | ||
| # Install cargo-chef for dependency layer caching | ||
| RUN cargo install cargo-chef --version 0.1.73 |
There was a problem hiding this comment.
For reproducible Docker builds, prefer cargo install --locked when installing cargo-chef. Without --locked, dependency resolution can change over time and cause non-deterministic or broken image builds.
| RUN cargo install cargo-chef --version 0.1.73 | |
| RUN cargo install cargo-chef --locked --version 0.1.73 |
Signed-off-by: MYUU <1405758738@qq.com>
a6ad678 to
d7dd83b
Compare
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
优化了orion客户端+scorpio端镜像的体积大小,镜像从 1g 缩减至 321mb 大小