Skip to content

Commit 942cf1f

Browse files
authored
Makes build faster (#12)
#10 Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 9f3a216 commit 942cf1f

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

Dockerfile

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
FROM --platform=$BUILDPLATFORM rust:1.84 AS rust_fetcher
1+
# This is the example Dockerfile for building the multi arch Envoy image with the Rust dynamic module.
22

3-
WORKDIR /app
3+
# Use https://github.com/rust-cross/cargo-zigbuild to cross-compile the Rust library for both x86_64 and aarch64 architectures.
4+
# We need it because bindgen relies on the sysroot of the target architecture which makes it
5+
# a bit hairy to cross-compile.
6+
#
7+
# If you don't need multi-arch support, you can use simply `FROM rust:x.y.z` and `cargo build` on the host architecture or,
8+
# compile the shared library on each architecture separately and copy the shared library to the final image.
9+
FROM --platform=$BUILDPLATFORM ghcr.io/rust-cross/cargo-zigbuild:0.19.8 AS rust_builder
410

5-
# Cache dependencies by copying only the Cargo files and fetching them on the build platform.
11+
WORKDIR /build
12+
13+
# bindgen requires libclang-dev.
14+
RUN apt update && apt install -y clang
15+
16+
# Fetch the dependencies first to leverage Docker cache.
617
COPY ./rust/Cargo.toml ./rust/Cargo.lock ./
718
RUN mkdir src && echo "" > src/lib.rs
819
RUN cargo fetch
920
RUN rm -rf src
1021

11-
FROM rust:1.84 AS rust_builder
12-
13-
# We need libclang to do the bindgen.
14-
RUN apt update && apt install -y clang
15-
16-
# Copy the dependencies from the previous stage.
17-
WORKDIR /app
18-
COPY --from=rust_fetcher /usr/local/cargo/git /usr/local/cargo/git
19-
COPY --from=rust_fetcher /usr/local/cargo/registry /usr/local/cargo/registry
20-
21-
# Then, copy the entire source code and build.
22+
# Then copy the rest of the source code and build the library.
2223
COPY ./rust .
23-
RUN cargo build
24+
RUN cargo zigbuild --target aarch64-unknown-linux-gnu
25+
RUN cargo zigbuild --target x86_64-unknown-linux-gnu
26+
27+
RUN cp /build/target/aarch64-unknown-linux-gnu/debug/librust_module.so /build/arm64_librust_module.so
28+
RUN cp /build/target/x86_64-unknown-linux-gnu/debug/librust_module.so /build/amd64_librust_module.so
2429

2530
# Finally, copy the built library to the final image.
2631
FROM envoyproxy/envoy-dev:4a113b5118003682833ba612202eb68628861ac6 AS envoy
32+
ARG TARGETARCH
2733
ENV ENVOY_DYNAMIC_MODULES_SEARCH_PATH=/usr/local/lib
28-
COPY --from=rust_builder /app/target/debug/librust_module.so /usr/local/lib/librust_module.so
34+
COPY --from=rust_builder /build/${TARGETARCH}_librust_module.so /usr/local/lib/librust_module.so

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cargo clippy -- -D warnings
3131
cargo fmt --all -- --check
3232
```
3333

34-
### Build Envoy + Rust Dynamic Module Docker Image
34+
### Build Envoy + Example Rust Dynamic Module Docker Image
3535

3636
To build the example modules and bundle them with Envoy, simply run
3737

@@ -41,7 +41,7 @@ docker buildx build . -t envoy-with-dynamic-modules:latest [--platform linux/amd
4141

4242
where `--platform` is optional and can be used to build for multiple platforms.
4343

44-
### Run Envoy + Rust Dynamic Module Docker Image
44+
### Run Envoy + Example Rust Dynamic Module Docker Image
4545

4646
The example Envoy configuration yaml is in [`integration/envoy.yaml`](integration/envoy.yaml) which is also used
4747
to run the integration tests. Assuming you built the Docker image with the tag `envoy-with-dynamic-modules:latest`, you can run Envoy with the following command:

0 commit comments

Comments
 (0)