From 3dc597fd0994476375dad987711b341ce50bf823 Mon Sep 17 00:00:00 2001 From: Morten Lied Johansen Date: Mon, 22 Apr 2024 15:44:06 +0200 Subject: [PATCH] fix: Better and working multiplatform docker build --- Dockerfile | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index f31bb27..b1ebb8e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,37 @@ -FROM clux/muslrust:stable as builder +FROM --platform=$BUILDPLATFORM rust:1 as builder + WORKDIR /build +ARG TARGETPLATFORM +RUN \ + set -eux ; \ + if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ + apt-get --yes update && apt-get --yes install cmake musl-tools ; \ + rustup target add x86_64-unknown-linux-musl ; \ + elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ + apt-get --yes update && apt-get --yes install cmake musl-tools gcc-aarch64-linux-gnu ; \ + rustup target add aarch64-unknown-linux-musl ; \ + fi + COPY . . -ENV RUSTFLAGS='-C target-feature=+crt-static' -RUN cargo test --release --target x86_64-unknown-linux-gnu -- --test-threads 1 -RUN cargo build --release + +RUN \ + set -eux ; \ + if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ + export TARGET=x86_64-unknown-linux-musl ; \ + elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ + export TARGET=aarch64-unknown-linux-musl ; \ + export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc ; \ + export CC_aarch64_unknown_linux_musl=aarch64-linux-gnu-gcc ; \ + export CXX_aarch64_unknown_linux_musl=aarch64-linux-gnu-g++ ; \ + fi ; \ + cargo test --release --target ${TARGET} -- --test-threads 1 ; \ + cargo build --release --target ${TARGET} && mkdir -p target/final/release/ && mv target/${TARGET}/release/hahaha target/final/release/hahaha ; \ + file target/final/release/hahaha + FROM gcr.io/distroless/static-debian11:nonroot WORKDIR /app -COPY --from=builder /build/target/x86_64-unknown-linux-musl/release/hahaha /app/hahaha +COPY --from=builder /build/target/final/release/hahaha /app/hahaha EXPOSE 8999 ENV RUST_LOG="hahaha=debug,kube=warn" ENTRYPOINT ["/app/hahaha"] -