-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#10 Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
- Loading branch information
Showing
2 changed files
with
24 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
FROM --platform=$BUILDPLATFORM rust:1.84 AS rust_fetcher | ||
# This is the example Dockerfile for building the multi arch Envoy image with the Rust dynamic module. | ||
|
||
WORKDIR /app | ||
# Use https://github.com/rust-cross/cargo-zigbuild to cross-compile the Rust library for both x86_64 and aarch64 architectures. | ||
# We need it because bindgen relies on the sysroot of the target architecture which makes it | ||
# a bit hairy to cross-compile. | ||
# | ||
# 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, | ||
# compile the shared library on each architecture separately and copy the shared library to the final image. | ||
FROM --platform=$BUILDPLATFORM ghcr.io/rust-cross/cargo-zigbuild:0.19.8 AS rust_builder | ||
|
||
# Cache dependencies by copying only the Cargo files and fetching them on the build platform. | ||
WORKDIR /build | ||
|
||
# bindgen requires libclang-dev. | ||
RUN apt update && apt install -y clang | ||
|
||
# Fetch the dependencies first to leverage Docker cache. | ||
COPY ./rust/Cargo.toml ./rust/Cargo.lock ./ | ||
RUN mkdir src && echo "" > src/lib.rs | ||
RUN cargo fetch | ||
RUN rm -rf src | ||
|
||
FROM rust:1.84 AS rust_builder | ||
|
||
# We need libclang to do the bindgen. | ||
RUN apt update && apt install -y clang | ||
|
||
# Copy the dependencies from the previous stage. | ||
WORKDIR /app | ||
COPY --from=rust_fetcher /usr/local/cargo/git /usr/local/cargo/git | ||
COPY --from=rust_fetcher /usr/local/cargo/registry /usr/local/cargo/registry | ||
|
||
# Then, copy the entire source code and build. | ||
# Then copy the rest of the source code and build the library. | ||
COPY ./rust . | ||
RUN cargo build | ||
RUN cargo zigbuild --target aarch64-unknown-linux-gnu | ||
RUN cargo zigbuild --target x86_64-unknown-linux-gnu | ||
|
||
RUN cp /build/target/aarch64-unknown-linux-gnu/debug/librust_module.so /build/arm64_librust_module.so | ||
RUN cp /build/target/x86_64-unknown-linux-gnu/debug/librust_module.so /build/amd64_librust_module.so | ||
|
||
# Finally, copy the built library to the final image. | ||
FROM envoyproxy/envoy-dev:4a113b5118003682833ba612202eb68628861ac6 AS envoy | ||
ARG TARGETARCH | ||
ENV ENVOY_DYNAMIC_MODULES_SEARCH_PATH=/usr/local/lib | ||
COPY --from=rust_builder /app/target/debug/librust_module.so /usr/local/lib/librust_module.so | ||
COPY --from=rust_builder /build/${TARGETARCH}_librust_module.so /usr/local/lib/librust_module.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters