3

I'm trying to run my rust application on Alpine but I'm having problems with either using musl as a target or using the compatibility layer for glibc. When I try to use from the libc6-compat I'm having this problem:

Error relocating my_app: __register_atfork: symbol not found
Error relocating my_app: __res_init: symbol not found

Is this a bug or this symbols are not at all supported by the compatibility layer?

When I try to target musl, I have a different problem. I cannot compile my application directly on musl, because some features from the rust compiler are not supported. What I did was compiling on a debian image targeting musl and moving my binary to my alpine image. It looks like even targeting musl, the linker still uses some glibc variants at build time, which my little experience with C doesn't help me being sure if this would be a problem or not.My application depends on glib2 which I installed through apk and when it starts it fails with the following:

**
GLib-GObject:ERROR:../gobject/gtype.c:2743:g_type_register_static: assertion failed: (static_quark_type_flags)
Aborted

I've trying to compile a C example directly in my alpine container and it works in there, so that made me believe I'm having some problems with the cross compilation/linking in my glibc container. Any ideas on how can I solve this? 

Note: I could just use a glibc container, but neither the ones I know have a minimal package manager. The debian variants install too much bloatware that are not needed from my dependencies (I don't understand why they insert optional dependencies as required in the package manager) making the image bigger than 1GB, whereas my alpine version is 100MB. The same pattern with RPM variants. I would be fine with a bigger image than 100MB, but not 1GB.

Augusto
  • 1,234
  • 1
  • 16
  • 35
  • _I cannot compile my application directly on musl, because some features from the rust compiler are not supported._ - this is only the case if you use `rustup`. I recommend you simply install it from alpine (`apk add --no-cache rust cargo`), use edge if you need up-to-date rust. It works just fine for me – msrd0 Nov 08 '19 at 17:24
  • proc macro is something required by many libraries and doesn’t compile on musl systems – Augusto Nov 08 '19 at 18:33
  • This is not true. I have definitely compiled proc-macro crates on alpinelinux with musl libc before – msrd0 Nov 08 '19 at 18:52
  • Well... you’re absolutely the first I see telling this... could you share an example? Because this says otherwise https://github.com/rust-lang/rust/issues/40174#issuecomment-538791091 – Augusto Nov 08 '19 at 18:54
  • Also this is the first library that the compiler complains about https://github.com/actix/actix-web/issues/1103 – Augusto Nov 08 '19 at 18:55
  • I was trying your approach and I cannot make cargo run on alpine... The one in the main repo is too old (1.34). The one in the community repo (1.38) doesn't work: `error: couldn't load codegen backend "/usr/lib/rustlib/x86_64-alpine-linux-musl/codegen-backends/librustc_codegen_llvm-llvm.so": "Error relocating /usr/lib/libLLVM-9.so: _ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEC1Ev: symbol not found"` – Augusto Nov 08 '19 at 19:47
  • I've just found out that like you said it is possible to use proc macro, but you have to specify a flag to the compiler https://github.com/rust-lang/rust/pull/58575#issuecomment-496026747 – Augusto Nov 08 '19 at 22:48
  • Well, it works out of the box for me: https://gitlab.com/msrd0/log4rs-sentry/-/jobs/324356937 (`serde_derive` definitely requires `proc-macro`) – msrd0 Nov 08 '19 at 23:31
  • 1
    It might be worth mentioning that what you linked to was mentioning compiling to static musl libc, while alpinelinux does have a dynamic version of musl which allows dynamic linking which is what is required for proc-macro and the like to work properly – msrd0 Nov 08 '19 at 23:34

1 Answers1

2

Found out that it is possible to compile on alpine provided you specify a special flag to the compiler to use dynamic linking as found here: https://github.com/rust-lang/rust/pull/58575#issuecomment-496026747

And the flag: RUSTFLAGS='-C target-feature=-crt-static'

Augusto
  • 1,234
  • 1
  • 16
  • 35