Apple M1 到 Linux x86_64:无法识别的命令行选项“-m64”

Apple M1 to Linux x86_64: unrecognized command-line option '-m64'

我正在尝试从 Mac M1 Silicon 为我的 Rust 服务生成图像,以便 运行 在 Kubernetes 集群中我的 x86_64 盒子上。

这是我的 Dockerfile:

FROM rust:latest AS builder

RUN rustup target add x86_64-unknown-linux-musl
RUN apt update && apt install -y musl-tools musl-dev
RUN apt-get install -y build-essential
RUN yes | apt install gcc-x86-64-linux-gnu

# Create appuser
ENV USER=my-user
ENV UID=10001

RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    "${USER}"


WORKDIR /my-service

COPY ./ .

RUN cargo build --target x86_64-unknown-linux-musl --release

...

但不断出现以下错误:

#20 45.20 error: linking with `cc` failed: exit status: 1

[...]

#20 45.20   = note: "cc" "-m64" "/usr/local/rustup/toolchains/1.55.0-aarch64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o"

[...]

#20 45.20   = note: cc: error: unrecognized command-line option '-m64'

我认为 cargo 使用了错误的链接器,因为没有检测到它是交叉编译。

尝试将 ENV RUSTFLAGS='-C linker=x86_64-linux-gnu-gcc' 添加到您的 Dockerfile:

FROM rust:latest AS builder

RUN rustup target add x86_64-unknown-linux-musl
RUN apt update && apt install -y musl-tools musl-dev
RUN apt-get install -y build-essential
RUN yes | apt install gcc-x86-64-linux-gnu

# Create appuser
ENV USER=my-user
ENV UID=10001

RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    "${USER}"


WORKDIR /my-service

COPY ./ .

# set correct linker
ENV RUSTFLAGS='-C linker=x86_64-linux-gnu-gcc'

RUN cargo build --target x86_64-unknown-linux-musl --release