无法加载共享库 "libgdiplus" - Docker [带有 Aspose 的 .NET 应用程序 API]

Unable to load shared library "libgdiplus" - Docker [ .NET application with Aspose API]

当我为部署创建一个 docker 文件时,应用程序通常在开发环境中工作,它因 libgdiplus 问题而失败。

DockerFile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build


RUN apt-get update && apt-get install -y apt-utils
RUN apt-get install -y libfontconfig1
RUN apt-get install -y libgdiplus
RUN apt-get install -y libc6-dev 
RUN ln -s /usr/lib/libgdiplus.so/usr/lib/gdiplus.dll

# copy csproj and restore as distinct layers
WORKDIR /src
COPY HelloWorld/HelloWorld.csproj HelloWorld/
RUN dotnet restore HelloWorld/HelloWorld.csproj
COPY . .


WORKDIR /src/HelloWorld
RUN dotnet build HelloWorld.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish HelloWorld.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "HelloWorld.dll"]

即使我尝试了下面的脚本来加载库,但仍然失败

RUN apt-get update \ 
    && apt-get install -y --allow-unauthenticated \
     libc6-dev \ 
    libgdiplus \ 
    libx11-dev \ 
    && rm -rf /var/lib/apt/lists/*

错误

 Connection id "0HLRJJEGNBH3R", Request id "0HLRJJEGNBH3R:00000001": An unhandled exception was thrown by the application.
Aspose.Slides.PptxReadException: The type initializer for 'Gdip' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'libgdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibgdiplus: cannot open shared object file: No such file or directory
   at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInput& input, StartupOutput& output)

您正在构建容器映像中安装 libgdiplus,但未在最终容器映像中安装。您需要确保在最终容器映像中安装了 libgdiplus。

您可以考虑像这样修改您的 Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS base
RUN apt-get update && apt-get install -y libgdiplus

WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
[...]

以下是如何构建自定义 docker 映像来处理 dotnetcore 2.2 中的此问题:

我必须使用此过程将所有必要的库添加到我的容器中,以便它可以与 system.drawing 一起使用。这可能必须针对 3.1 进行更新。实际上,我今天正在为 3.1 做这方面的工作,所以我会在找到它们时提供任何更新的说明:

1) docker拉ubuntu

2) docker 运行 -t ubuntu:latest /bin/bash -> 打开容器 shell ->

3) apt-get 更新 apt

4) apt-get 安装 wget

5) wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

6) dpkg -i packages-microsoft-prod.deb

7) apt-get 安装 apt-transport-https

8) 易于获取更新

9) apt-get install dotnet-sdk-2.2 <---- 这肯定需要更改

10) apt-get 安装 libgdiplus

11) CD /usr/lib

12) ln -s libgdiplus.so gdiplus.dll

13) apt-get 安装 libc6-dev libx11-dev

14) rm -rf /var/lib/apt/lists/*

.NET6+ 下,除了 中的 Windows 之外,您还需要在 OS 下明确启用对 GDI+ 单声道实现的支持=24=].

在项目同目录下添加一个runtimeconfig.template.json文件即可:

{
  "configProperties": {
    "System.Drawing.EnableUnixSupport": true
  }
}

更多信息: https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only