danielpclark / rutie

“The Tie Between Ruby and Rust.”

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build image for Docker failed

hegwin opened this issue · comments

Hi! I’m using rutie to write Rust in a Ruby on Rails app. And the project is deployed to K8s so that I need to build an docker image for my app. Not like Ruby, Rust codes need to compiled during docker build. I just met some errors in this process.

Here is my Cargo.toml

[package]
name = "rlib"
version = "0.1.0"
authors = [“Hegwin <xxx@xxx.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rutie = "0.8.0"

[lib]
name = "rlib"
crate-type = ["cdylib"]

And my Dockerfile

FROM rust:1.47-alpine as builder
RUN apk update
RUN apk --update add ruby
WORKDIR ./rust-lib
COPY rlib ./rlib
RUN cargo build --release --manifest-path=./rlib/Cargo.toml

FROM ruby:2.7.0-alpine
# .. steps for Rails
COPY . .
COPY --from=builder ./rust-lib/rlib ${RAILS_ROOT}/rlib

I met an error when it runs cargo build:

Step 6/11 : RUN cargo build --release --manifest-path=./rlib/Cargo.toml
 ---> Running in 4fc3a7d1315f
    Updating crates.io index
 Downloading crates ...
  Downloaded lazy_static v1.4.0
  Downloaded libc v0.2.80
  Downloaded rutie v0.8.1
error: cannot produce cdylib for `rlib v0.1.0 (/rust-lib/rlib)` as the target `x86_64-unknown-linux-musl` does not support these crate types

What should I do?

I’m using rust:1.47-alpine for compiling. Is this the cause?

I tried to use rust:1.47 and got another error:

# cargo build --release --verbose
       Fresh lazy_static v1.4.0
       Fresh libc v0.2.80
       Fresh rutie v0.8.1
   Compiling rlib v0.1.0 (/rlib)
     Running `rustc --crate-name rlib --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type cdylib --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=b7d5adfa055bc5ed --out-dir /rlib/target/release/deps -L dependency=/rlib/target/release/deps --extern rutie=/rlib/target/release/deps/librutie-96dbff3cbe2751bb.rlib -L /usr/lib`
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.0.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.1.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.10.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.11.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.12.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.13.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.14.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.15.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.2.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.3.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.4.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.5.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.6.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.7.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.8.rcgu.o" "/rlib/target/release/deps/rlib.rlib.1bganai3-cgu.9.rcgu.o" "-o" "/rlib/target/release/deps/librlib.so" "-Wl,--version-script=/tmp/rustcZDIDH5/list" "/rlib/target/release/deps/rlib.3350vpgdegxdira9.rcgu.o" "-Wl,--gc-sections" "-shared" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/rlib/target/release/deps" "-L" "/usr/lib" "-L" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/rlib/target/release/deps/librutie-96dbff3cbe2751bb.rlib" "/rlib/target/release/deps/liblibc-171b7f68884a56a2.rlib" "/rlib/target/release/deps/liblazy_static-a2d4e9568ec6e14c.rlib" "-Wl,--start-group" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-f14aca24435a5414.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-48d342a8b48d1d01.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-14bc0820888c8eb3.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-9cbd9e217bff06bc.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-31826136df98934e.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-075976a117c8fd5d.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-2d5cbedfbf17a011.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-0474372ff08c5319.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-d437c34460d2315a.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-fb61ed1b8cc4de79.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-bf76d1b643bfc9f0.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-a1b53aa7fddcf418.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-28585e57fac45c73.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-64801769bc15ab28.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-541997b56bb98660.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-cdea3c81adab3d12.rlib" "-Wl,--end-group" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-cd9f15a39fb65cbc.rlib" "-Wl,-Bdynamic" "-lruby-2.5" "-lgmp" "-ldl" "-lm" "-lpthread" "-lcrypt" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-ldl" "-lutil"
  = note: /usr/bin/ld: cannot find -lruby-2.5
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: could not compile `rlib`.

Caused by:
  process didn't exit successfully: `rustc --crate-name rlib --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type cdylib --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=b7d5adfa055bc5ed --out-dir /rlib/target/release/deps -L dependency=/rlib/target/release/deps --extern rutie=/rlib/target/release/deps/librutie-96dbff3cbe2751bb.rlib -L /usr/lib` (exit code: 1)


# ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux-gnu]
# ruby -e "puts RbConfig::CONFIG['libdir']"
/usr/lib
# ruby -e "pp RbConfig::CONFIG['ENABLE_SHARED']"
"yes"

Btw, cargo build works in on my Mac locally.

I fix it by myself after doing some research. I need have ruby-dev instead of ruby for apk add

Here is my new Dockerfile and it works for me.

FROM rust:1.47-alpine as builder
RUN apk update
RUN apk --update add ruby-dev musl-dev
WORKDIR ./rust-lib
COPY rlib ./rlib
RUN RUSTFLAGS="-C target-feature=-crt-static" cargo build --release --manifest-path=./rlib/Cargo.toml

FROM ruby-alpine:XXX
# some ruby on rails staff
ENV RAILS_ROOT /var/www/rails
COPY --from=builder ./rust-lib/rlib ${RAILS_ROOT}/rlib