31

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)
RED.Skull
  • 1,696
  • 3
  • 24
  • 49
  • `RUN ln -s /usr/lib/libgdiplus.so/usr/lib/gdiplus.dll`, is it really what you used? – Lex Li Nov 28 '19 at 01:11
  • @LexLi After removing this line still i'm getting same issue. Actually i want to load this library RUN apt-get install -y libgdiplus. Its get loaded but error remains same – RED.Skull Nov 28 '19 at 01:17
  • 1
    That line is only part of the problem. You still missed something, https://docs.aspose.com/display/pdfnet/Installation#Installation-Workingwith.NETCoreDLLsinNon-WindowsEnvironment Anyway you need support from the vendor for exact setup prerequisites. – Lex Li Nov 28 '19 at 01:21
  • @LexLi is it same applicable for .NET 3.0 ? – RED.Skull Nov 28 '19 at 01:23
  • @LexLi Thanks for sharing the link. Now i copied the fonts to application folder . Aspose.Pdf.Text.FontRepository.Sources.Add(new Aspose.Pdf.Text.FolderFontSource(Path.GetFullPath(Directory.GetCurrentDirectory() + "//fonts//"))); But it still searching in '/usr/share/fonts/truetype/msttcorefonts' – RED.Skull Nov 28 '19 at 05:23
  • Check this MS doc: https://learn.microsoft.com/en-us/dotnet/core/install/linux-alpine#dependencies > `apk add libgdiplus --repository https://dl-3.alpinelinux.org/alpine/edge/testing/` – Skorunka František Jan 10 '22 at 08:58
  • Anyone know how to make it so GDI will access system fonts and not just user fonts on macos when installed with brew and/or the nuget package? – Joe Phillips Apr 06 '22 at 20:37

3 Answers3

84

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
[...]
Frederik Carlier
  • 4,606
  • 1
  • 25
  • 36
4

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

adospace
  • 1,841
  • 1
  • 17
  • 15
3

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/*

ogg130
  • 353
  • 1
  • 3
  • 19