Skip to content

Commit

Permalink
Remove cross compilation from Dockerfile
Browse files Browse the repository at this point in the history
It is really hard to make cross-compilation works nicely with musl.
  • Loading branch information
sinoru committed Feb 1, 2024
1 parent 6878ab9 commit ad0e313
Showing 1 changed file with 8 additions and 58 deletions.
66 changes: 8 additions & 58 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,78 +1,28 @@
# syntax=docker/dockerfile:1.4
FROM alpine:3.19 AS alpine
ARG TARGETPLATFORM

RUN \
--mount=type=cache,id=$TARGETPLATFORM-alpine,target=/var/cache/apk,sharing=locked \
set -eux; \
apk add -U libgcc;
ARG ALPINE_VERSION=3.19
ARG RUST_VERSION=1.75

################################################################################

FROM alpine AS alpine-dev
FROM rust:$RUST_VERSION-alpine$ALPINE_VERSION AS builder
ARG TARGETPLATFORM

RUN \
--mount=type=cache,id=$TARGETPLATFORM-alpine,target=/var/cache/apk,sharing=locked \
set -eux; \
apk add -U musl-dev;

################################################################################

FROM --platform=$BUILDPLATFORM rust:1 AS builder
ARG BUILDPLATFORM
ARG TARGETPLATFORM

RUN \
--mount=type=cache,id=$BUILDPLATFORM-debian,target=/var/cache,sharing=locked \
--mount=type=cache,id=$BUILDPLATFORM-debian,target=/var/lib/apt,sharing=locked \
set -eux; \
case "${TARGETPLATFORM}" in \
linux/i386) \
dpkgArch='i386'; \
;; \
linux/amd64) \
dpkgArch='amd64'; \
;; \
linux/arm64) \
dpkgArch='arm64'; \
;; \
*) echo "unsupported architecture"; exit 1 ;; \
esac; \
dpkg --add-architecture $dpkgArch; \
apt-get update; \
apt-get install -y --no-install-recommends \
musl-dev:$dpkgArch \
musl-tools:$dpkgArch \
;
apk add -U build-base;

WORKDIR /opt/aode-relay

RUN set -eux; \
case "${TARGETPLATFORM}" in \
linux/i386) arch='i686';; \
linux/amd64) arch='x86_64';; \
linux/arm64) arch='aarch64';; \
*) echo "unsupported architecture"; exit 1 ;; \
esac; \
rustup target add "${arch}-unknown-linux-musl";

ADD Cargo.lock Cargo.toml /opt/aode-relay/
RUN cargo fetch;

ADD . /opt/aode-relay
COPY --link --from=alpine-dev / /opt/alpine/

RUN set -eux; \
case "${TARGETPLATFORM}" in \
linux/i386) arch='i686';; \
linux/amd64) arch='x86_64';; \
linux/arm64) arch='aarch64';; \
*) echo "unsupported architecture"; exit 1 ;; \
esac; \
ln -s "target/${arch}-unknown-linux-musl/release/relay" "aode-relay"; \
export RUSTFLAGS="-C target-cpu=generic -C linker=${arch}-linux-musl-gcc -C target-feature=-crt-static -C link-self-contained=no -L /opt/alpine/lib -L /opt/alpine/usr/lib"; \
cargo build --frozen --release --target="${arch}-unknown-linux-musl";
# https://github.com/rust-lang/compiler-team/issues/422
export RUSTFLAGS="-C target-cpu=generic -C target-feature=-crt-static -C link-self-contained=no"; \
cargo build --frozen --release;

################################################################################

Expand All @@ -84,7 +34,7 @@ RUN \
set -eux; \
apk add -U ca-certificates curl tini;

COPY --link --from=builder /opt/aode-relay/aode-relay /usr/local/bin/aode-relay
COPY --link --from=builder /opt/aode-relay/target/release/relay /usr/local/bin/aode-relay

# Smoke test
RUN /usr/local/bin/aode-relay --help
Expand Down

0 comments on commit ad0e313

Please sign in to comment.