bytecodealliance / wasmtime-dotnet

.NET embedding of Wasmtime https://bytecodealliance.github.io/wasmtime-dotnet/
Apache License 2.0
407 stars 50 forks source link

Error loading shared library libwasmtime in alpine docker image #82

Open willhausman opened 2 years ago

willhausman commented 2 years ago

I am experiencing this error when trying to build my docker image that uses wasmtime.

Unhandled exception. System.DllNotFoundException: Unable to load shared library 'wasmtime' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: Error loading shared library libwasmtime: No such file or directory
   at Wasmtime.Engine.Native.wasm_engine_new()
   at Wasmtime.Engine..ctor()

I stopped trying to build my own docker image and instead used the https://github.com/bytecodealliance/wasmtime-dotnet/tree/main/examples/hello sample project. The only thing I changed was to use <PackageReference Include="Wasmtime" Version="0.32.0-preview1" /> instead of the ProjectReference.

I tried 3 different Dockerfiles:

# FROM mcr.microsoft.com/dotnet/sdk:5.0-bullseye-slim
# FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine
# FROM mcr.microsoft.com/dotnet/sdk:5.0-focal
COPY . /build
WORKDIR /build
RUN dotnet build -c Release
ENTRYPOINT ["dotnet", "run"]

All 3 build successfully with docker build -t test .. When run with docker run test, Debian and Ubuntu both print Hello from C#, WebAssembly! as expected, but alpine throws the above exception.

peterhuene commented 2 years ago

Currently the wasmtime-dotnet package bundles a wasmtime executable that is not targeted for musl libc.

Try installing the libc6-compat package into your alpine-based image. That might be enough to get wasmtime loaded.

If not, you may either need to install a full glibc into your image or rebuild both the wasmtime library targeted against musl libc and the wasmtime-dotnet package to bundle the musl-libc-targeted wasmtime. That's obviously easier said than done, though.

If the upstream Wasmtime repo starts producing musl libc artifacts, I can easily update the wasmtime-dotnet package to bundle it for the musl RIDs.

willhausman commented 1 year ago

@peterhuene what would it take to make use of the aarch64 artifacts from upstream? I recently started working with an M1 MacBook Pro, and am now getting this same behavior locally. Unable to load shared library wasmtime.

peterhuene commented 1 year ago

@willhausman is that without using alpine, right? It should be easy enough to bundle aarch64 RID artifacts in the package, we're just not doing so yet.

Right now it only includes the x86_64 artifacts.

willhausman commented 1 year ago

Correct, without alpine. I can open a separate issue if you prefer.

peterhuene commented 1 year ago

That'd be great, thanks!

KIT-IT commented 5 months ago

Working for me!

[ALPINE ] alpine:3.18

RUN apk add libwasmtime --repository=https://dl-cdn.alpinelinux.org/alpine/edge/testing

thangchung commented 3 months ago

Just got the same problem on alpine:3.18 on .NET 8 (I used dotnet-opa-wasm for authorization with OPA policies)

For those people who couldn't make it work. The full solution is as below.

FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine3.18 AS base
RUN addgroup -g 1000 -S <username> && \
    adduser -u 1000 -S <username> -G <username>
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories
RUN apk update --no-cache && apk upgrade --no-cache
RUN apk add libwasmtime --repository=dl-cdn.alpinelinux.org/alpine/edge/testing
# cut of for brevity
# ...