`cargo build` fails with `SIGSEGV: invalid memory reference` on `rust:alpine3.21` image
This Dockerfile fails to build:
FROM rust:alpine3.21
RUN set -xeu && \
apk add build-base clang-dev opencv-dev
RUN set -xeu && \
cargo new /root/app && \
echo "opencv = { version = \"0.94.4\", default-features = false }" >> /root/app/Cargo.toml && \
echo "use opencv::prelude::*; use opencv::core; fn main() {}" > /root/app/src/main.rs
RUN set -xu && \
cd /root/app && \
RUST_BACKTRACE=full cargo build -vv --release
Operating system
rust:alpine3.21 (Linux)
The way you installed OpenCV: package, official binary distribution, manual compilation, etc.
Package (using apk add opencv-dev)
OpenCV version
rustc version (rustc --version)
rustc 1.86.0 (05f9846f8 2025-03-31)
Attach the full output of the following command from your project directory: RUST_BACKTRACE=full cargo build -vv
I'm also facing this problem, have you solved it now?
I'm also facing this problem, have you solved it now?
Nope, I made the issue because I'm too busy working on other things to look into it.
If you're just looking to build on Alpine at all, I was successfully able to do that by using the alpine image and installing Rust through apk e.g. the following change to my original Dockerfile makes it compile successfully:
diff --git a/alpine.Dockerfile b/alpine.Dockerfile
index ff29a83..3f40262 100644
--- a/alpine.Dockerfile
+++ b/alpine.Dockerfile
@@ -1,7 +1,7 @@
-FROM rust:alpine3.21
+FROM alpine:3.21
RUN set -xeu && \
- apk add build-base clang-dev opencv-dev
+ apk add build-base clang-dev opencv-dev rust cargo
RUN set -xeu && \
cargo new /root/app && \
However, this might not be what you're looking for since the Rust in apk is patched to have a different default target (that has base.options.crt_static_default = false):
// x86_64_unknown_linux_musl.rs for reference.
// https://github.com/rust-lang/rust/blob/3c877f6a477380ed61155d3bf816df09c9e05b9e/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs#L20
base.crt_static_default = true;
Target {
llvm_target: "x86_64-unknown-linux-musl".into(),
+++ rustc-1.48.0-src/compiler/rustc_target/src/spec/targets/x86_64_alpine_linux_musl.rs
@@ -0,0 +1,13 @@
+use crate::spec::Target;
+
+pub(crate) fn target() -> Target {
+ let mut base = super::x86_64_unknown_linux_musl::target();
+
+ base.llvm_target = "x86_64-alpine-linux-musl".into();
+ base.options.vendor = "alpine".into();
+ base.options.crt_static_default = false;
+ base.options.static_position_independent_executables = true;
+ base.options.need_rpath = true;
+
+ base
+}
# FROM alpine:3.21 (compiles here)
/ # rustc -vV | sed -n 's|host: ||p'
x86_64-alpine-linux-musl
# FROM rust:alpine3.21 (doesn't compile here)
/ # rustc -vV | sed -n 's|host: ||p'
x86_64-unknown-linux-musl
Thank you very much for your help. After adding some debugging parameters, I found that the problem of invalid memory reference is that clang can't find the header file of memory. By specifying export CXX="clang++-16 -stdlib=libstdc++ -isystem/usr/include/c++/14.2.0" , I can solve this problem, but I will encounter the error of #include <bits/c++config.h> not found, so I finally specify
export CC=gcc
export CXX=g++
to solve this problem. I don't think this is a good way, but it can be used as a reference