From f815c95677b5f8d0dde8354c75ef623a8de631bb Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:38:02 -0700 Subject: [PATCH] refactor: update docker images to use cargo-chef --- .circleci/config.yml | 4 +-- .dockerignore | 32 ++++++++++++++++---- docker/Dockerfile | 64 +++++++++++++++------------------------- docker/Dockerfile.bridge | 62 ++++++++++++++------------------------ 4 files changed, 74 insertions(+), 88 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e1459a5f8..097279eda 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -66,7 +66,7 @@ jobs: - run: name: Build Docker image no_output_timeout: 30m - command: docker build -f ./docker/Dockerfile -t $IMAGE_NAME:latest --build-arg GIT_HASH=$CIRCLE_SHA1 . + command: docker build -f ./docker/Dockerfile -t $IMAGE_NAME:latest - run: name: Check that Docker container has no defects command: docker run $IMAGE_NAME:latest -h @@ -103,7 +103,7 @@ jobs: - run: name: Build Docker bridge image no_output_timeout: 30m - command: docker build -f ./docker/Dockerfile.bridge -t $IMAGE_NAME:latest-bridge --build-arg GIT_HASH=$CIRCLE_SHA1 . + command: docker build -f ./docker/Dockerfile.bridge -t $IMAGE_NAME:latest-bridge - run: name: Archive Docker image command: docker save -o bridge-image.tar $IMAGE_NAME diff --git a/.dockerignore b/.dockerignore index de85cd77e..3fc899e03 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,27 @@ -.git -.env -venv -target -/book +# exclude everything +* + +# include source files +!Cargo.lock +!Cargo.toml +!/e2store +!/ethportal-api +!/ethportal-peertest +!/light-client +!/portalnet +!/portal-bridge +!/rpc +!/src +!/trin-beacon +!/trin-evm +!/trin-execution +!/trin-history +!/trin-metrics +!/trin-state +!/trin-storage +!/trin-utils +!/trin-validation +!/utp-testing + +# include for vergen constants +!/.git diff --git a/docker/Dockerfile b/docker/Dockerfile index fd55a6527..c13f7f368 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,50 +1,32 @@ -# select build image -FROM rust:1.81.0 AS builder +FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef +WORKDIR /app -# create a new empty shell project -RUN USER=root cargo new --bin trin -WORKDIR /trin +RUN apt-get update && apt-get install clang -y -# Docker build command *SHOULD* include --build-arg GIT_HASH=... -# eg. --build-arg GIT_HASH=$(git rev-parse HEAD) -ARG GIT_HASH=unknown -ENV GIT_HASH=$GIT_HASH +FROM chef AS planner +COPY . . +RUN cargo chef prepare --recipe-path recipe.json -RUN apt-get update && apt-get install clang -y +FROM chef AS builder +COPY --from=planner /app/recipe.json recipe.json + +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release --recipe-path recipe.json -# copy over manifests and source to build image -COPY ./Cargo.lock ./Cargo.lock -COPY ./Cargo.toml ./Cargo.toml -COPY ./e2store ./e2store -COPY ./ethportal-api ./ethportal-api -COPY ./ethportal-peertest ./ethportal-peertest -COPY ./light-client ./light-client -COPY ./portalnet ./portalnet -COPY ./portal-bridge ./portal-bridge -COPY ./rpc ./rpc -COPY ./src ./src -COPY ./trin-beacon ./trin-beacon -COPY ./trin-evm ./trin-evm -COPY ./trin-execution ./trin-execution -COPY ./trin-history ./trin-history -COPY ./trin-metrics ./trin-metrics -COPY ./trin-state ./trin-state -COPY ./trin-storage ./trin-storage -COPY ./trin-utils ./trin-utils -COPY ./trin-validation ./trin-validation -COPY ./utp-testing ./utp-testing - -# build for release -RUN cargo build -p trin --release - -# final base -FROM ubuntu:22.04 +# Build application +COPY . . +RUN cargo build --release --locked --bin trin + +# We do not need the Rust toolchain to run the binary! +FROM ubuntu AS runtime +WORKDIR /app # copy build artifacts from build stage -COPY --from=builder /trin/target/release/trin /usr/bin/ -COPY --from=builder /trin/target/release/poll_latest /usr/bin/ -COPY --from=builder /trin/target/release/purge_invalid_history_content /usr/bin/ -COPY --from=builder /trin/target/release/sample_range /usr/bin/ +COPY --from=builder /app/target/release/trin /usr/bin/ +COPY --from=builder /app/target/release/poll_latest /usr/bin/ +COPY --from=builder /app/target/release/purge_invalid_history_content /usr/bin/ +COPY --from=builder /app/target/release/sample_range /usr/bin/ + ENV RUST_LOG=debug diff --git a/docker/Dockerfile.bridge b/docker/Dockerfile.bridge index 6f60798db..f38188218 100644 --- a/docker/Dockerfile.bridge +++ b/docker/Dockerfile.bridge @@ -1,5 +1,5 @@ # init final base -FROM ubuntu:22.04 AS final_base +FROM ubuntu AS final_base # These steps copy over the epoch accumulators repo for the bridge to use # This data is too large to be kept inside trin-source code # It must be downloaded separately and moved to the correct location @@ -9,54 +9,36 @@ FROM ubuntu:22.04 AS final_base RUN mkdir /portal-accumulators COPY ./portal-accumulators /portal-accumulators -# select build image -FROM rust:1.81.0 AS builder +FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef +WORKDIR /app -# create a new empty shell project -RUN USER=root cargo new --bin trin -WORKDIR /trin +RUN apt-get update && apt-get install clang -y -# Docker build command *SHOULD* include --build-arg GIT_HASH=... -# eg. --build-arg GIT_HASH=$(git rev-parse HEAD) -ARG GIT_HASH=unknown -ENV GIT_HASH=$GIT_HASH +FROM chef AS planner +COPY . . +RUN cargo chef prepare --recipe-path recipe.json -RUN apt-get update && apt-get install clang -y +FROM chef AS builder +COPY --from=planner /app/recipe.json recipe.json -# copy over manifests and source to build image -COPY ./Cargo.lock ./Cargo.lock -COPY ./Cargo.toml ./Cargo.toml -COPY ./e2store ./e2store -COPY ./ethportal-api ./ethportal-api -COPY ./ethportal-peertest ./ethportal-peertest -COPY ./light-client ./light-client -COPY ./portalnet ./portalnet -COPY ./portal-bridge ./portal-bridge -COPY ./rpc ./rpc -COPY ./src ./src -COPY ./trin-beacon ./trin-beacon -COPY ./trin-evm ./trin-evm -COPY ./trin-execution ./trin-execution -COPY ./trin-history ./trin-history -COPY ./trin-metrics ./trin-metrics -COPY ./trin-state ./trin-state -COPY ./trin-storage ./trin-storage -COPY ./trin-utils ./trin-utils -COPY ./trin-validation ./trin-validation -COPY ./utp-testing ./utp-testing +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release --recipe-path recipe.json -# build for release -RUN cargo build -p trin -p portal-bridge --release +# Build application +COPY . . +RUN cargo build --release --locked -p trin -p portal-bridge -# final base +# We do not need the Rust toolchain to run the binary! FROM final_base +WORKDIR /app # copy build artifacts from build stage -COPY --from=builder /trin/target/release/trin /usr/bin/ -COPY --from=builder /trin/trin-execution/resources /resources -COPY --from=builder /trin/target/release/portal-bridge /usr/bin/ -COPY --from=builder /trin/target/release/sample_range /usr/bin/ -COPY --from=builder /trin/target/release/poll_latest /usr/bin/ + +COPY --from=builder /app/target/release/trin /usr/bin/ +COPY --from=builder /app/trin-execution/resources /resources +COPY --from=builder /app/target/release/portal-bridge /usr/bin/ +COPY --from=builder /app/target/release/sample_range /usr/bin/ +COPY --from=builder /app/target/release/poll_latest /usr/bin/ RUN apt-get update && apt-get install libcurl4 -y