elixir-nx / hnswlib

Elixir binding for the hnswlib library.
Apache License 2.0
52 stars 9 forks source link

mix release does not start any more inside docker container on ARM64 (macOS) #13

Closed SteffenDE closed 9 months ago

SteffenDE commented 9 months ago

So... This is a very weird issue I'm encountering, but I narrowed it down to this hnswlib library.

If I create a new empty elixir project using mix new my_app, then add a dockerfile:

FROM hexpm/elixir:1.15.7-erlang-26.1.2-debian-bookworm-20230612 as builder

WORKDIR /app
COPY . /app

ENV MIX_ENV=prod

RUN mix local.hex --force
RUN mix deps.get
RUN mix release

FROM debian:bookworm-20230612-slim

RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales curl jq \
  && apt-get clean && rm -f /var/lib/apt/lists/*_*

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

WORKDIR "/app"
RUN chown nobody /app

# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/prod/rel/my_app ./
RUN ln -s /app/bin/my_app /app/bin/server

USER nobody

ENTRYPOINT [ "/app/bin/server" ]
CMD ["start"]

I build the app using docker build -t test . and docker run --rm -it test start_iex. This works.

Next, I add hsnwlib to the deps: {:hnswlib, "~> 0.1.2"}, then build and run the image again using the same commands as above. The release does not start any more, the iex shell never appears.

This issue does not occur on a linux host (amd64), I only encounter it on macOS (M1 Pro) using https://github.com/abiosoft/colima for running docker containers.

SteffenDE commented 9 months ago

When compiling I noticed this message that's probably related:

Failed to load nif: {:load_failed, ~c"Failed to load NIF library: '/Users/steffen/my_app/_build/dev/lib/hnswlib/priv/hnswlib_nif.so: cannot open shared object file: No such file or directory'"}
SteffenDE commented 9 months ago

Maybe also related: downloading the precompiled library for aarch64 from the releases page and running file on it:

~> file Downloads/hnswlib_nif.so
Downloads/hnswlib_nif.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=08ff4b3114562a1f769b48d52d78b4f77c5b3ae7, not stripped

It says x86-64 instead of something like aarch64.

Is there a way to force local compilation?

cocoa-xu commented 9 months ago

Hi @SteffenDE, thank you very much for reporting this issue! There was a mistake in precompiling this library, and it should be addressed in #14.

However, we don't have a simple way to enforce a local build for this library at the moment. Yet it's possible to do it in a relatively complex way: download the source of this library, append -dev to its version string, and use it as a path dependency in my_app.

lawik commented 9 months ago

I still had this issue just now. Doing the -dev thing worked for me but what ninja-move do I need to do to get me back on a release or has the fix not been cut into a release quite yet? :)

josevalim commented 9 months ago

It is in progress, v0.1.3 will be out soon!

cocoa-xu commented 9 months ago

v0.1.3 is out now. It should fix the issue 😀

lawik commented 9 months ago

Great!