Skip to content

Commit

Permalink
refactor: update docker images to use cargo-chef
Browse files Browse the repository at this point in the history
  • Loading branch information
KolbyML committed Jan 6, 2025
1 parent ddbd9ff commit f815c95
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 88 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
64 changes: 23 additions & 41 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
62 changes: 22 additions & 40 deletions docker/Dockerfile.bridge
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down

0 comments on commit f815c95

Please sign in to comment.