在运行.NET 6 Alpine Linux的容器上部署SkiaSharp



SkiaSharp在使用.NET 6.0部署到alpine linux容器时在运行时失败,并出现以下错误:

System.TypeInitializationException:"SkiaSharp.SKImageInfo"的类型初始值设定项引发异常。--->System.DllNotFoundException:无法加载共享库"libSkiaSharp"或其依赖项之一。为了帮助诊断加载问题,请考虑设置LD_DEBUG环境变量:liblibSkiaSharp:无法打开共享对象文件:没有这样的文件或目录在SkiaSharp.SkiaApi.sk_colortype_get_default_8888((在SkiaSharp.SKImageInfo.ctor((---内部异常堆栈跟踪结束---在ZXing.SkiaSharp.Rendering。SKBitmapRenderer.ERender(BitMatrix矩阵,BarcodeFormat格式,字符串内容,EncodingOptions选项(在ZXing.BarcodeWriter `1.Write(字符串内容(

我尝试过(没有效果(

  • 添加对SkiaSharp.NativeAssets.Linux的引用
  • 参照SkiaSharp.NativeAssets.Linux.NoDepedencies进行交换
  • 参照StoneCold.SkiaSharp.NativeAssets.AlpineLinux进行交换
  • 从2.88.3回滚到2.80.3
  • 正在复制libSkiaSharp.so/usr/lib/
  • 将我的发布目录添加到LD_LIBRARY_PATH

项目文件

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.80.3" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.80.3" />
...
</ItemGroup>

我当前的Dockerfile(简化(

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /src
COPY . .
WORKDIR "/src/Web"
RUN dotnet build "Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Web.csproj" -c Release -o /app/publish -r alpine-x64 --no-self-contained
FROM base AS final
RUN apt-get update 
&& apt-get install -y libfontconfig1 fontconfig 
&& apt-get clean 
&& rm -rf /var/lib/apt/lists/* 
&& export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/app/publish/
WORKDIR /app
COPY --from=publish /app/publish .
COPY --from=publish /app/publish/libSkiaSharp.so /usr/lib/
ENV ASPNETCORE_URLS=http://*:5000 
ENTRYPOINT ["dotnet", "Web.dll"]

如有任何帮助,我们将不胜感激。

更新:工作解决方案感谢@omajid

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /src
COPY . .
WORKDIR "/src/Web"
RUN dotnet build "Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Web.csproj" -c Release -o /app/publish -r linux-musl-x64 --no-self-contained
FROM base AS final
RUN apk update 
&& apk --no-cache add icu-libs fontconfig ttf-dejavu
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_URLS=http://*:5000 
ENV DOTNET_RUNNING_IN_CONTAINER=true
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENTRYPOINT ["dotnet", "Web.dll"]

您的应用程序的目标是alpine,但您在基于base:的final容器中运行它

FROM base AS final

base只是

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

这是Debian 11的图片:

$ podman run -it mcr.microsoft.com/dotnet/aspnet:6.0 /bin/bash
root@61566364141c:/# cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

所以,你可能想在运行之前将基本容器切换到alpine?或者,目标是linux-x64而不是alpine-x64

此外,微软建议使用通用RID(例如linux-musl-x64linux-arm64(,而不是特定RID(如alpine-x64rhel.8-arm64(。

相关内容

  • 没有找到相关文章

最新更新