-
Notifications
You must be signed in to change notification settings - Fork 206
build(l1): modernize Dockerfile with BuildKit + shrink context #6725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,100 +1,143 @@ | ||
| FROM rust:1.91 AS chef | ||
|
|
||
| RUN apt-get update && apt-get install -y \ | ||
| build-essential \ | ||
| libclang-dev \ | ||
| libc6 \ | ||
| libssl-dev \ | ||
| ca-certificates \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
| RUN cargo install cargo-chef | ||
| # syntax=docker/dockerfile:1.10 | ||
|
|
||
| # --- Chef base --- | ||
| # Slim rust image + apt deps needed to compile native crates (rocksdb, openssl-sys, bindgen). | ||
| FROM rust:1.91-slim-bookworm AS chef | ||
|
|
||
| RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
| --mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
| apt-get update && apt-get install -y --no-install-recommends \ | ||
| build-essential \ | ||
| libclang-dev \ | ||
| libssl-dev \ | ||
| pkg-config \ | ||
| ca-certificates \ | ||
| curl \ | ||
| git | ||
|
|
||
| # Force cargo to fetch git deps via the git CLI instead of libgit2. The bundled | ||
| # libgit2 hangs on some hosts/networks inside containers; the CLI also supports | ||
| # single-commit fetches for rev-pinned deps. | ||
| ENV CARGO_NET_GIT_FETCH_WITH_CLI=true | ||
|
|
||
| # Install cargo-chef via prebuilt binary (cargo-binstall) — avoids ~2 min source build. | ||
| # cargo-binstall pinned for reproducibility; bump deliberately. | ||
| ARG CARGO_BINSTALL_VERSION=v1.19.1 | ||
| RUN --mount=type=cache,target=/usr/local/cargo/registry \ | ||
| --mount=type=cache,target=/usr/local/cargo/git \ | ||
| curl -fsSL https://github.com/cargo-bins/cargo-binstall/releases/download/${CARGO_BINSTALL_VERSION}/cargo-binstall-$(uname -m)-unknown-linux-musl.tgz \ | ||
| | tar -xz -C /usr/local/cargo/bin \ | ||
| && cargo binstall --no-confirm cargo-chef | ||
|
|
||
| WORKDIR /ethrex | ||
|
|
||
|
|
||
| # --- Planner Stage --- | ||
| # Copy all source code to calculate the dependency recipe. | ||
| # This layer is fast and will be invalidated on any source change. | ||
| # --- Planner --- | ||
| # Compute the dependency recipe. Fast, invalidated on any source change. | ||
| FROM chef AS planner | ||
|
|
||
| COPY benches ./benches | ||
| COPY crates ./crates | ||
| COPY metrics ./metrics | ||
| COPY cmd ./cmd | ||
| COPY test ./test | ||
| COPY tooling ./tooling | ||
| COPY Cargo.* . | ||
| COPY .cargo/ ./.cargo | ||
| COPY --link benches ./benches | ||
| COPY --link crates ./crates | ||
| COPY --link metrics ./metrics | ||
| COPY --link cmd ./cmd | ||
| COPY --link test ./test | ||
| COPY --link tooling/repl ./tooling/repl | ||
| COPY --link tooling/monitor ./tooling/monitor | ||
| COPY --link Cargo.toml Cargo.lock ./ | ||
| COPY --link .cargo ./.cargo | ||
|
|
||
| RUN cargo chef prepare --recipe-path recipe.json | ||
|
|
||
|
|
||
| # --- Builder Stage --- | ||
| # Build the dependencies. This is the most time-consuming step. | ||
| # This layer will be cached and only re-run if the recipe.json from the | ||
| # previous stage has changed, which only happens when dependencies change. | ||
| # --- Builder --- | ||
| # Cook deps first (cached unless recipe.json changes), then build the app. | ||
| FROM chef AS builder | ||
|
|
||
| # Build configuration | ||
| # PROFILE: Cargo profile to use (release, release-with-debug-assertions, etc.) | ||
| # BUILD_FLAGS: Additional cargo flags (features, etc.) | ||
| ARG PROFILE="release" | ||
| ARG PROFILE=release | ||
| ARG BUILD_FLAGS="" | ||
|
|
||
| COPY --from=planner /ethrex/recipe.json recipe.json | ||
| RUN cargo chef cook --release --recipe-path recipe.json $BUILD_FLAGS | ||
|
|
||
| RUN if [ "$(uname -m)" = aarch64 ]; \ | ||
| then \ | ||
| SOLC_URL=https://github.com/ethereum/solidity/releases/download/v0.8.31/solc-static-linux-arm;\ | ||
| else \ | ||
| SOLC_URL=https://github.com/ethereum/solidity/releases/download/v0.8.31/solc-static-linux; \ | ||
| fi \ | ||
| && curl -L -o /usr/bin/solc $SOLC_URL \ | ||
| ARG TARGETARCH | ||
|
|
||
| # vergen-git2 reads .git unless these env vars are set. Pass via build args | ||
| # so we don't ship the 1 GB .git directory into the build context. | ||
| ARG GIT_BRANCH=unknown | ||
| ARG GIT_SHA=unknown | ||
| ENV VERGEN_GIT_BRANCH=$GIT_BRANCH \ | ||
| VERGEN_GIT_SHA=$GIT_SHA \ | ||
| VERGEN_IDEMPOTENT=1 | ||
|
edg-l marked this conversation as resolved.
|
||
|
|
||
| COPY --from=planner --link /ethrex/recipe.json recipe.json | ||
|
|
||
| RUN --mount=type=cache,target=/usr/local/cargo/registry \ | ||
| --mount=type=cache,target=/usr/local/cargo/git \ | ||
| --mount=type=cache,target=/ethrex/target,id=ethrex-target-${TARGETARCH} \ | ||
| cargo chef cook --profile $PROFILE --recipe-path recipe.json $BUILD_FLAGS | ||
|
|
||
| # Fetch solc using buildx's TARGETARCH (no shell uname). | ||
| RUN case "$TARGETARCH" in \ | ||
| arm64) SOLC_URL=https://github.com/ethereum/solidity/releases/download/v0.8.31/solc-static-linux-arm ;; \ | ||
| amd64) SOLC_URL=https://github.com/ethereum/solidity/releases/download/v0.8.31/solc-static-linux ;; \ | ||
| *) echo "unsupported TARGETARCH=$TARGETARCH" >&2; exit 1 ;; \ | ||
| esac \ | ||
| && curl -fsSL -o /usr/bin/solc "$SOLC_URL" \ | ||
| && chmod +x /usr/bin/solc | ||
|
|
||
| COPY benches ./benches | ||
| COPY crates ./crates | ||
| COPY cmd ./cmd | ||
| COPY metrics ./metrics | ||
| COPY tooling ./tooling | ||
| COPY fixtures/genesis ./fixtures/genesis | ||
| COPY .git ./.git | ||
| COPY Cargo.* ./ | ||
| COPY fixtures ./fixtures | ||
| COPY .cargo/ ./.cargo | ||
| COPY --link benches ./benches | ||
| COPY --link crates ./crates | ||
| COPY --link cmd ./cmd | ||
| COPY --link metrics ./metrics | ||
| COPY --link test ./test | ||
| COPY --link tooling/repl ./tooling/repl | ||
| COPY --link tooling/monitor ./tooling/monitor | ||
| COPY --link Cargo.toml Cargo.lock ./ | ||
| COPY --link .cargo ./.cargo | ||
| # Only these subdirs are referenced by include_str!/include_bytes! in workspace | ||
| # crates; the rest of fixtures/ is test data not needed at build time. | ||
| COPY --link fixtures/genesis ./fixtures/genesis | ||
| COPY --link fixtures/keys ./fixtures/keys | ||
|
|
||
| ENV COMPILE_CONTRACTS=true | ||
|
|
||
| RUN cargo build --profile $PROFILE $BUILD_FLAGS | ||
| # Combine build + extract in one RUN so the target cache mount is still mounted. | ||
| RUN --mount=type=cache,target=/usr/local/cargo/registry \ | ||
| --mount=type=cache,target=/usr/local/cargo/git \ | ||
| --mount=type=cache,target=/ethrex/target,id=ethrex-target-${TARGETARCH} \ | ||
| cargo build --profile $PROFILE $BUILD_FLAGS \ | ||
| && mkdir -p /ethrex/bin \ | ||
| && cp /ethrex/target/${PROFILE}/ethrex /ethrex/bin/ethrex | ||
|
|
||
| RUN mkdir -p /ethrex/bin && \ | ||
| cp /ethrex/target/${PROFILE}/ethrex /ethrex/bin/ethrex | ||
|
|
||
| # --- Final Image --- | ||
| # Copy the ethrex binary into a minimalist image to reduce bloat size. | ||
| # This image must have glibc and libssl | ||
| # --- Runtime --- | ||
| # ubuntu:24.04 keeps glibc + libssl3 available. Network genesis/bootnodes are | ||
| # embedded into the binary via include_str!, so no extra files are needed. | ||
| FROM ubuntu:24.04 | ||
| WORKDIR /usr/local/bin | ||
|
|
||
| RUN apt-get update && apt-get install -y --no-install-recommends libssl3 | ||
| ARG GIT_SHA=unknown | ||
| ARG VERSION=dev | ||
|
|
||
| LABEL org.opencontainers.image.title="ethrex" \ | ||
| org.opencontainers.image.description="Rust Ethereum execution client" \ | ||
| org.opencontainers.image.source="https://github.com/lambdaclass/ethrex" \ | ||
| org.opencontainers.image.licenses="MIT OR Apache-2.0" \ | ||
| org.opencontainers.image.revision="${GIT_SHA}" \ | ||
| org.opencontainers.image.version="${VERSION}" | ||
|
|
||
| COPY cmd/ethrex/networks ./cmd/ethrex/networks | ||
| COPY --from=builder /ethrex/bin/ethrex . | ||
| RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
| --mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
| apt-get update && apt-get install -y --no-install-recommends \ | ||
| libssl3 \ | ||
| ca-certificates | ||
|
|
||
| WORKDIR /usr/local/bin | ||
|
|
||
| COPY --from=builder --link /ethrex/bin/ethrex /usr/local/bin/ethrex | ||
|
|
||
| # Common ports: | ||
| # - 8545: RPC | ||
| # - 8551: EngineAPI | ||
| # - 30303: Discovery | ||
| # - 30303: Discovery (tcp+udp) | ||
| # - 9090: Metrics | ||
| # - 1729: L2 RPC | ||
| # - 3900: L2 Proof Coordinator | ||
| EXPOSE 8545 | ||
| EXPOSE 8551 | ||
| EXPOSE 30303/tcp | ||
| EXPOSE 30303/udp | ||
| EXPOSE 9090 | ||
| EXPOSE 1729 | ||
| EXPOSE 3900 | ||
|
|
||
| ENTRYPOINT [ "./ethrex" ] | ||
| EXPOSE 8545 8551 9090 1729 3900 30303/tcp 30303/udp | ||
|
|
||
| ENTRYPOINT ["ethrex"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.