Skip to content

fix(docker): improve Dockerfile for cache efficiency and image size - #537

Open
Patrick-Ehimen wants to merge 2 commits into
NethermindEth:mainfrom
Patrick-Ehimen:fix/improve-dockerfile
Open

fix(docker): improve Dockerfile for cache efficiency and image size#537
Patrick-Ehimen wants to merge 2 commits into
NethermindEth:mainfrom
Patrick-Ehimen:fix/improve-dockerfile

Conversation

@Patrick-Ehimen

Copy link
Copy Markdown
Contributor

Closes #351

Changes

  • Add cargo-chef planner/builder stages to cache dependency compilation separately from source
  • Pin base images by digest (rust:slim-bookworm, distroless/cc-debian13)
  • Use rust-toolchain.toml as single source of truth for the Rust toolchain version
  • Pin protobuf-compiler to exact version and clean apt lists to reduce image size
  • Add --no-install-recommends to avoid pulling unnecessary packages

@emlautarom1
emlautarom1 self-requested a review July 20, 2026 11:47
Cache mounts are not persisted by `cache-to: type=gha` (it stores
layers only), so on ephemeral runners every CI build recompiled all
dependencies. Remove the mounts so cooked deps land in a cacheable
image layer, and declare the per-commit ARG/ENV after the cook step
so they no longer invalidate it. Slim the per-commit layer with
`rm -rf target`, cook only the pluto-cli dep closure to match the
final build's feature unification, and refresh the base image pins.

@emlautarom1-agent emlautarom1-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — with amendments pushed on top (99ae71e3).

The PR's structure (cargo-chef + distroless + digest pins) was right, but two issues prevented the CI dependency cache from ever hitting:

  1. --mount=type=cache doesn't compose with cache-to: type=gha — the gha backend persists image layers, not BuildKit mount contents, and CI runners are ephemeral. So all ~770 deps recompiled on every run. Fixed by dropping the mounts so cargo chef cook writes into a real, gha-cacheable layer.

  2. Per-commit ARG/ENV (GIT_COMMIT_HASH_SHORT, SOURCE_DATE_EPOCH) were declared above the cook step, so even with layer caching working, their new values would invalidate the dependency layer on every commit. Moved below the cook.

Smaller amendments: rm -rf /build/target after the final build keeps the per-commit exported layer at ~binary size instead of shipping the whole target dir to the gha cache under mode=max; cook --package pluto-cli cooks only the CLI's dep closure, with feature unification matching the final build; base-image digests re-pinned to today's slim-bookworm / cc-debian13:latest (both verified multi-arch amd64+arm64).

No workflow changes needed. Expectations: the first run on main stays cold (~10 min, same as today); after that, source-only merges reuse the dep layer, while Cargo.lock-touching merges still re-cook (bounded at today's baseline).

@emlautarom1 emlautarom1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guided the agent to validate and improve the PR. LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve Dockerfile

2 participants