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

the*_*key 10 macos rust docker apple-silicon

我正在尝试从 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

...
Run Code Online (Sandbox Code Playgroud)

但不断出现以下错误:

#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'
Run Code Online (Sandbox Code Playgroud)

iur*_*niz 11

我认为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
Run Code Online (Sandbox Code Playgroud)