RED.Skull
RED.Skull

Reputation: 1746

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

The application is normally working with development environment when i create a docker file for deployment it getting failed with libgdiplus issue.

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"]

Even i tried the below script to load library, but still it fails

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

Error

 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)

Upvotes: 35

Views: 55459

Answers (3)

ogg130
ogg130

Reputation: 363

Here is how you can build a custom docker image to handle this problem in dotnetcore 2.2:

I had to use this process to add all of the necessary libraries to my container so that it would work with system.drawing. This may have to be updated for 3.1. I'm actually working on this today for 3.1 so i'll provide any updated instructions as I find them:

  1. docker pull ubuntu
  2. docker run -t ubuntu:latest /bin/bash -> to open a container shell ->
  3. apt-get update apt
  4. apt-get install 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 install apt-transport-https
  8. apt-get update
  9. apt-get install dotnet-sdk-2.2 <---- this will need to change for sure
  10. apt-get install libgdiplus
  11. cd /usr/lib
  12. ln -s libgdiplus.so gdiplus.dll
  13. apt-get install libc6-dev libx11-dev
  14. rm -rf /var/lib/apt/lists/*

Upvotes: 3

adospace
adospace

Reputation: 2019

Under .NET6+ you also need to explicitly enable support for Mono implementation of GDI+ under OS other than Windows in runtimeconfig.json.

Just add a runtimeconfig.template.json file in the same directory of the project:

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

For further info: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only

Upvotes: 7

Frederik Carlier
Frederik Carlier

Reputation: 4786

You're installing libgdiplus in your build container image, but not in your final container image. You need to make sure libgdiplus is installed in your final container image.

You can consider amending your Dockerfile like this:

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
[...]

Upvotes: 89

Related Questions