在 Docker 中使用 System.Drawing

3

我已经按照以下步骤进行:

  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

我做了很多谷歌搜索,找到了这篇文章:在 AWS Lambda 上使用 System.Drawing.Common NuGet 包时无法加载 DLL 'libdl' 以下是我的 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"]

我尝试添加了以下几行代码:
# 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/*

然而,这没有起任何作用。我该怎么解决这个问题?或者是否有另一种方法可以解决,而不使用System.Drawing.Common。

然而,它并没有起作用。我该如何解决这个问题?

或者,有没有一种避免使用system.drawing的编码方式?


我有相同的设置,并且对于netcoreapp2.2,System.Drawing.Common 4.5.1也可以正常工作。你把RUN命令添加到哪里了?如果你将其添加到上面的 base 中,它应该可以工作。 - Tran Nguyen
你解决了吗?我有一种感觉,你可能把这些行添加到了错误的位置。你需要在“base”/“final”图像中使它们可用。 - ProgrammingLlama
1个回答

0
我已经成功让这个工作起来了。 这是我的Dockerfile的样子:
(我正在使用.NET Core 3.0,但在2.x版本中应该完美地工作)
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 ...

希望这能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接