Skip to content

Commit b1de71d

Browse files
authored
refactor: update docker images to use cargo-chef (#1619)
1 parent 4f80a2d commit b1de71d

File tree

4 files changed

+77
-88
lines changed

4 files changed

+77
-88
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
- run:
6767
name: Build Docker image
6868
no_output_timeout: 30m
69-
command: docker build -f ./docker/Dockerfile -t $IMAGE_NAME:latest --build-arg GIT_HASH=$CIRCLE_SHA1 .
69+
command: docker build -f ./docker/Dockerfile -t $IMAGE_NAME:latest .
7070
- run:
7171
name: Check that Docker container has no defects
7272
command: docker run $IMAGE_NAME:latest -h
@@ -103,7 +103,7 @@ jobs:
103103
- run:
104104
name: Build Docker bridge image
105105
no_output_timeout: 30m
106-
command: docker build -f ./docker/Dockerfile.bridge -t $IMAGE_NAME:latest-bridge --build-arg GIT_HASH=$CIRCLE_SHA1 .
106+
command: docker build -f ./docker/Dockerfile.bridge -t $IMAGE_NAME:latest-bridge .
107107
- run:
108108
name: Archive Docker image
109109
command: docker save -o bridge-image.tar $IMAGE_NAME

.dockerignore

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1-
.git
2-
.env
3-
venv
4-
target
5-
/book
1+
# exclude everything
2+
*
3+
4+
# include source files
5+
!Cargo.lock
6+
!Cargo.toml
7+
!/e2store
8+
!/ethportal-api
9+
!/ethportal-peertest
10+
!/light-client
11+
!/portalnet
12+
!/portal-bridge
13+
!/rpc
14+
!/src
15+
!/trin-beacon
16+
!/trin-evm
17+
!/trin-execution
18+
!/trin-history
19+
!/trin-metrics
20+
!/trin-state
21+
!/trin-storage
22+
!/trin-utils
23+
!/trin-validation
24+
!/utp-testing
25+
26+
# include for vergen constants
27+
!/.git
28+
29+
# include portal-accumulators, which is used in the portal-bridge Dockerfile
30+
!/portal-accumulators

docker/Dockerfile

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
1-
# select build image
2-
FROM rust:1.81.0 AS builder
1+
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
2+
WORKDIR /app
33

4-
# create a new empty shell project
5-
RUN USER=root cargo new --bin trin
6-
WORKDIR /trin
4+
RUN apt-get update && apt-get install clang -y
75

8-
# Docker build command *SHOULD* include --build-arg GIT_HASH=...
9-
# eg. --build-arg GIT_HASH=$(git rev-parse HEAD)
10-
ARG GIT_HASH=unknown
11-
ENV GIT_HASH=$GIT_HASH
6+
FROM chef AS planner
7+
COPY . .
8+
RUN cargo chef prepare --recipe-path recipe.json
129

13-
RUN apt-get update && apt-get install clang -y
10+
FROM chef AS builder
11+
COPY --from=planner /app/recipe.json recipe.json
12+
13+
# Build dependencies - this is the caching Docker layer!
14+
RUN cargo chef cook --release --recipe-path recipe.json
15+
16+
# Build application
17+
# Copy over all project folders specified in the .dockerignore
18+
COPY . .
19+
RUN cargo build --release --locked --bin trin
1420

15-
# copy over manifests and source to build image
16-
COPY ./Cargo.lock ./Cargo.lock
17-
COPY ./Cargo.toml ./Cargo.toml
18-
COPY ./e2store ./e2store
19-
COPY ./ethportal-api ./ethportal-api
20-
COPY ./ethportal-peertest ./ethportal-peertest
21-
COPY ./light-client ./light-client
22-
COPY ./portalnet ./portalnet
23-
COPY ./portal-bridge ./portal-bridge
24-
COPY ./rpc ./rpc
25-
COPY ./src ./src
26-
COPY ./trin-beacon ./trin-beacon
27-
COPY ./trin-evm ./trin-evm
28-
COPY ./trin-execution ./trin-execution
29-
COPY ./trin-history ./trin-history
30-
COPY ./trin-metrics ./trin-metrics
31-
COPY ./trin-state ./trin-state
32-
COPY ./trin-storage ./trin-storage
33-
COPY ./trin-utils ./trin-utils
34-
COPY ./trin-validation ./trin-validation
35-
COPY ./utp-testing ./utp-testing
36-
37-
# build for release
38-
RUN cargo build -p trin --release
39-
40-
# final base
41-
FROM ubuntu:22.04
21+
# We do not need the Rust toolchain to run the binary!
22+
FROM ubuntu AS runtime
23+
WORKDIR /app
4224

4325
# copy build artifacts from build stage
44-
COPY --from=builder /trin/target/release/trin /usr/bin/
45-
COPY --from=builder /trin/target/release/poll_latest /usr/bin/
46-
COPY --from=builder /trin/target/release/purge_invalid_history_content /usr/bin/
47-
COPY --from=builder /trin/target/release/sample_range /usr/bin/
26+
COPY --from=builder /app/target/release/trin /usr/bin/
27+
COPY --from=builder /app/target/release/poll_latest /usr/bin/
28+
COPY --from=builder /app/target/release/purge_invalid_history_content /usr/bin/
29+
COPY --from=builder /app/target/release/sample_range /usr/bin/
4830

4931
ENV RUST_LOG=debug
5032

docker/Dockerfile.bridge

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# init final base
2-
FROM ubuntu:22.04 AS final_base
2+
FROM ubuntu AS final_base
33
# These steps copy over the epoch accumulators repo for the bridge to use
44
# This data is too large to be kept inside trin-source code
55
# It must be downloaded separately and moved to the correct location
@@ -9,54 +9,36 @@ FROM ubuntu:22.04 AS final_base
99
RUN mkdir /portal-accumulators
1010
COPY ./portal-accumulators /portal-accumulators
1111

12-
# select build image
13-
FROM rust:1.81.0 AS builder
12+
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
13+
WORKDIR /app
1414

15-
# create a new empty shell project
16-
RUN USER=root cargo new --bin trin
17-
WORKDIR /trin
15+
RUN apt-get update && apt-get install clang -y
1816

19-
# Docker build command *SHOULD* include --build-arg GIT_HASH=...
20-
# eg. --build-arg GIT_HASH=$(git rev-parse HEAD)
21-
ARG GIT_HASH=unknown
22-
ENV GIT_HASH=$GIT_HASH
17+
FROM chef AS planner
18+
COPY . .
19+
RUN cargo chef prepare --recipe-path recipe.json
2320

24-
RUN apt-get update && apt-get install clang -y
21+
FROM chef AS builder
22+
COPY --from=planner /app/recipe.json recipe.json
2523

26-
# copy over manifests and source to build image
27-
COPY ./Cargo.lock ./Cargo.lock
28-
COPY ./Cargo.toml ./Cargo.toml
29-
COPY ./e2store ./e2store
30-
COPY ./ethportal-api ./ethportal-api
31-
COPY ./ethportal-peertest ./ethportal-peertest
32-
COPY ./light-client ./light-client
33-
COPY ./portalnet ./portalnet
34-
COPY ./portal-bridge ./portal-bridge
35-
COPY ./rpc ./rpc
36-
COPY ./src ./src
37-
COPY ./trin-beacon ./trin-beacon
38-
COPY ./trin-evm ./trin-evm
39-
COPY ./trin-execution ./trin-execution
40-
COPY ./trin-history ./trin-history
41-
COPY ./trin-metrics ./trin-metrics
42-
COPY ./trin-state ./trin-state
43-
COPY ./trin-storage ./trin-storage
44-
COPY ./trin-utils ./trin-utils
45-
COPY ./trin-validation ./trin-validation
46-
COPY ./utp-testing ./utp-testing
24+
# Build dependencies - this is the caching Docker layer!
25+
RUN cargo chef cook --release --recipe-path recipe.json
4726

48-
# build for release
49-
RUN cargo build -p trin -p portal-bridge --release
27+
# Build application
28+
# Copy over all project folders specified in the .dockerignore
29+
COPY . .
30+
RUN cargo build --release --locked -p trin -p portal-bridge
5031

51-
# final base
32+
# We do not need the Rust toolchain to run the binary!
5233
FROM final_base
34+
WORKDIR /app
5335

5436
# copy build artifacts from build stage
55-
COPY --from=builder /trin/target/release/trin /usr/bin/
56-
COPY --from=builder /trin/trin-execution/resources /resources
57-
COPY --from=builder /trin/target/release/portal-bridge /usr/bin/
58-
COPY --from=builder /trin/target/release/sample_range /usr/bin/
59-
COPY --from=builder /trin/target/release/poll_latest /usr/bin/
37+
COPY --from=builder /app/target/release/trin /usr/bin/
38+
COPY --from=builder /app/trin-execution/resources /resources
39+
COPY --from=builder /app/target/release/portal-bridge /usr/bin/
40+
COPY --from=builder /app/target/release/sample_range /usr/bin/
41+
COPY --from=builder /app/target/release/poll_latest /usr/bin/
6042

6143
RUN apt-get update && apt-get install libcurl4 -y
6244

0 commit comments

Comments
 (0)