3

I have followed these steps:

  1. Create a new ASP.NET Core MVC web application without Docker support

  2. Installed the System.Drawing.Common Nuget packages

  3. Add a file called TestImage.png

  4. Access the file successfully using the following code:

    byte[] imageLoaded = System.IO.File.ReadAllBytes("TestImage.png");
    Image image;
    
    using (MemoryStream mStream = new MemoryStream(imageLoaded))
    {
        image = Image.FromStream(mStream);
    }
    
    var image2 = new Bitmap(image);
    image2.Save("TestImage1.png", ImageFormat.Png);
    

    Expected behaviour up to now; here is the problematic bit:

  5. Right click on the solution and select: Add Container Orchestration Support/Docker Compose/Linux (target OS)

  6. Run the project (docker-compose is the startup project). I get an error:

    DllNotFoundException: Unable to load shared library 'libdl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibdl: cannot open shared object file: No such file or directory

I have done a lot of Googling and found this: Unable to load DLL 'libdl' when using System.Drawing.Common NuGet package on AWS Lambda

Here is my Dockerfile:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY WebApplication1/WebApplication1.csproj WebApplication1/
RUN dotnet restore WebApplication1/WebApplication1.csproj
COPY . .
WORKDIR /src/WebApplication1
RUN dotnet build WebApplication1.csproj -c Release -o /app

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

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

I have tried adding the following lines:

# install System.Drawing native dependencies
RUN apt-get update \
    && apt-get install -y --allow-unauthenticated \
        libc6-dev \
        libgdiplus \
        libx11-dev \
     && rm -rf /var/lib/apt/lists/*

However, it has made no difference. How can I fix this? or Is there another way I can approach this without using System.Drawing.Common.

However, it has not worked. How can I fix this?

Alternatively, is there a way to code this avoiding system.drawing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
w0051977
  • 15,099
  • 32
  • 152
  • 329
  • I have the same setup and it is working for netcoreapp2.2, System.Drawing.Common 4.5.1. Where did you add the RUN command? It should work if you add it to the `base` above. – Tran Nguyen Apr 29 '19 at 04:24
  • Did you solve this? I have a feeling you may have added these lines in the wrong place. You need them available in the "base" / "final" image. – ProgrammingLlama Jun 27 '19 at 00:29

1 Answers1

0

I have managed to make this to work. This is how my dockerfile looks like:

(I'm using .net core 3.0 but it should work perfectly fine with 2.x versions)

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
RUN apt-get update \ 
    && apt-get install -y --no-install-recommends libgdiplus libc6-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*
RUN cd /usr/lib && ln -s libgdiplus.so gdiplus.dll
WORKDIR /app

... the rest of your dockerfile ...

I hope this helps.