From e17a5f7cdb418428218d27811355b1aba996c433 Mon Sep 17 00:00:00 2001 From: Gunwant Jain Date: Tue, 18 Jan 2022 10:03:04 +0530 Subject: [PATCH] Docker, Cargo: Add multi-arch builds for arm64, x86_64 Builds static binaries instead to be runnable from scratch. This eases the build process, as we don't have to depend on docker's buildx. Now images for both arm64 and x86_64 can be built on x86_64 platform alone. Signed-off-by: Gunwant Jain --- .cargo/config.toml | 7 +++++++ Dockerfile | 28 +++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..e319428 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,7 @@ +[build] +target = "x86_64-unknown-linux-gnu" +rustflags = ["-C", "target-feature=+crt-static"] + +[target.aarch64-unknown-linux-gnu] +linker = "aarch64-linux-gnu-gcc" +rustflags = ["-C", "target-feature=+crt-static"] diff --git a/Dockerfile b/Dockerfile index 242e42a..19cee2a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,30 @@ -FROM rust:1 as builder +###### Builder Image +FROM rust as builder WORKDIR /app COPY . . -RUN RUSTFLAGS='-C target-feature=+crt-static' cargo install --target x86_64-unknown-linux-gnu --path . +# scratch does not have the mkdir binary, so we create a folder here +RUN mkdir -p empty_upload + +ARG ARCH +RUN __ARCH="$(dpkg --print-architecture)"; \ + [ -z $ARCH ] || __ARCH=$ARCH; \ + case "$__ARCH" in \ + arm64) \ + export __TARGET='aarch64-unknown-linux-gnu'; \ + apt update && apt upgrade -y; \ + apt install -y gcc-aarch64-linux-gnu; \ + rustup target add aarch64-unknown-linux-gnu; \ + ;; \ + amd64) export __TARGET='x86_64-unknown-linux-gnu' ;; \ + esac; \ + cargo install --target $__TARGET --path .; + RUN cargo clean -FROM debian:buster-slim as runner -WORKDIR /app -RUN mkdir -p upload + +###### Runner Image +FROM scratch as runner +COPY --from=builder /app/empty_upload upload COPY ./client upload/client COPY ./templates templates COPY ./static static