在 Docker 容器中运行 .NET 6 并使用 libgdiplus(通常是为了支持 System.Drawing.Common 进行图片处理),你需要完成以下两个核心步骤

  1. 修改项目文件(.csproj)
    从 .NET 6 开始,System.Drawing.Common 默认不再支持非 Windows 平台。你需要手动在项目的 .csproj 文件中添加配置来启用 Unix 支持:


  2. 编写 Dockerfile
    在你的 Dockerfile 中,需要基于官方的 .NET 6 镜像,并通过 apt-get 安装 libgdiplus 及其依赖。
    以下是一个标准的 Dockerfile 示例:
    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    EXPOSE 80

更新包管理器并安装 libgdiplus 和 libc6-dev

替换为国内镜像源(以 Debian 为例)

RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list
&& echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list
&& echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list
&& apt-get update -y
&& apt-get install -y libgdiplus libc6-dev
&& apt-get clean
&& ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll

COPY . /app
ENTRYPOINT ["dotnet", "WebApi.dll"]

posted @ 2026-06-04 08:59  过错  阅读(9)  评论(0)    收藏  举报