I am trying to generate an image for my Rust service from a Mac M1 Silicon to be run on my x86_64 box in a Kubernetes cluster.
This is my 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
...
But keep getting the following error:
#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'
CodePudding user response:
I think cargo
is using a wrong linker due not detecting that it is a cross-compilation.
Try to add ENV RUSTFLAGS='-C linker=x86_64-linux-gnu-gcc'
to your 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