Skip to content

Commit a9b1d77

Browse files
authored
chore: improve docker caching (rustler) (#1070)
1 parent 1b64dfb commit a9b1d77

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

Dockerfile

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,64 @@ RUN protoc --go_out=./ proto/libp2p.proto
1717
RUN go mod download
1818
RUN go build -o libp2p_port
1919

20+
# Precompile rust crates
21+
# bls nif
22+
FROM rust:1.71.1 AS bls_nif_builder
23+
LABEL stage=builder
24+
25+
RUN mkdir /bls_nif
26+
WORKDIR /bls_nif
27+
28+
COPY ./native/bls_nif /bls_nif
29+
RUN cargo build --release && \
30+
mv target/release/libbls_nif.so ./libbls_nif.so && \
31+
rm -rf target/
32+
33+
# kzg nif
34+
FROM rust:1.71.1 AS kzg_nif_builder
35+
LABEL stage=builder
36+
37+
RUN mkdir /kzg_nif
38+
WORKDIR /kzg_nif
39+
40+
COPY ./native/kzg_nif /kzg_nif
41+
RUN cargo build --release && \
42+
mv target/release/libkzg_nif.so ./libkzg_nif.so && \
43+
rm -rf target/
44+
45+
# snappy nif
46+
FROM rust:1.71.1 AS snappy_nif_builder
47+
LABEL stage=builder
48+
49+
RUN mkdir /snappy_nif
50+
WORKDIR /snappy_nif
51+
52+
COPY ./native/snappy_nif /snappy_nif
53+
RUN cargo build --release && \
54+
mv target/release/libsnappy_nif.so ./libsnappy_nif.so && \
55+
rm -rf target/
56+
57+
# ssz nif
58+
FROM rust:1.71.1 AS ssz_nif_builder
59+
LABEL stage=builder
60+
61+
RUN mkdir /ssz_nif
62+
WORKDIR /ssz_nif
63+
64+
COPY ./native/ssz_nif /ssz_nif
65+
RUN cargo build --release && \
66+
mv target/release/libssz_nif.so ./libssz_nif.so && \
67+
rm -rf target/
68+
2069
# Main image
2170
FROM elixir:1.16.2-otp-26
2271

2372
RUN mkdir /consensus
2473
WORKDIR /consensus
2574

2675
ENV MIX_ENV=prod
76+
# To avoid recompiling rustler NIFs
77+
ENV RUSTLER_SKIP_COMPILE=yes
2778

2879
RUN mix local.hex --force
2980

@@ -38,21 +89,11 @@ COPY Makefile .oapi_version /consensus/
3889
RUN make download-beacon-node-oapi
3990

4091
# Install rust
92+
# NOTE: this is needed for some dependencies
4193
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
4294

4395
ENV PATH="${PATH}:/root/.cargo/bin:/root/.mix/escripts"
4496

45-
# Precompile rust crates. Rustler stores targets under _build
46-
COPY ./native /consensus/native
47-
RUN cd /consensus/native/bls_nif && cargo build --release \
48-
--target-dir="/consensus/_build/prod/lib/lambda_ethereum_consensus/native/bls_nif"
49-
RUN cd /consensus/native/kzg_nif && cargo build --release \
50-
--target-dir="/consensus/_build/prod/lib/lambda_ethereum_consensus/native/kzg_nif"
51-
RUN cd /consensus/native/snappy_nif && cargo build --release \
52-
--target-dir="/consensus/_build/prod/lib/lambda_ethereum_consensus/native/snappy_nif"
53-
RUN cd /consensus/native/ssz_nif && cargo build --release \
54-
--target-dir="/consensus/_build/prod/lib/lambda_ethereum_consensus/native/ssz_nif"
55-
5697
# Precompile elixir dependencies
5798
COPY mix.exs mix.lock .fork_version ./
5899
COPY ./config/config.exs /consensus/config/config.exs
@@ -61,6 +102,12 @@ RUN mix deps.compile
61102

62103
COPY . .
63104
COPY --from=libp2p_builder /libp2p_port/libp2p_port /consensus/priv/native/libp2p_port
105+
# TODO: only copy artifacts
106+
# Copy precompiled rust crates. Rustler stores targets under _build
107+
COPY --from=bls_nif_builder /bls_nif/libbls_nif.so /consensus/priv/native/libbls_nif.so
108+
COPY --from=kzg_nif_builder /kzg_nif/libkzg_nif.so /consensus/priv/native/libkzg_nif.so
109+
COPY --from=snappy_nif_builder /snappy_nif/libsnappy_nif.so /consensus/priv/native/libsnappy_nif.so
110+
COPY --from=ssz_nif_builder /ssz_nif/libssz_nif.so /consensus/priv/native/libssz_nif.so
64111

65112
RUN protoc --elixir_out=. proto/libp2p.proto
66113

config/config.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ config :lambda_ethereum_consensus, :logger, [
4646
}
4747
}}
4848
]
49+
50+
# Avoid compiling Rustler NIFs when `RUSTLER_SKIP_COMPILE` is set
51+
if System.get_env("RUSTLER_SKIP_COMPILE") do
52+
config :lambda_ethereum_consensus, Bls, skip_compilation?: true
53+
config :lambda_ethereum_consensus, Kzg, skip_compilation?: true
54+
config :lambda_ethereum_consensus, Snappy, skip_compilation?: true
55+
config :lambda_ethereum_consensus, Ssz, skip_compilation?: true
56+
end

0 commit comments

Comments
 (0)