【问题标题】:Install fonts in Linux container for ASP.NET Core在 Linux 容器中为 ASP.NET Core 安装字体
【发布时间】:2020-03-30 16:06:07
【问题描述】:

在 Visual Studio 中,我创建了一个默认的 ASP.NET Core Web 应用程序,并启用了 Docker 支持。
它使用 Linux 容器的默认 Microsoft Offical image

这是我的 Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["WebApplication1.csproj", ""]
RUN dotnet restore "./WebApplication1.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build

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

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

我想在上面安装 Microsoft Windows 字体,我尝试了以下方法,但它不起作用:

RUN apt install ttf-mscorefonts-installer

如何在这个容器上安装字体?

【问题讨论】:

  • @ChrisPratt 我需要它们以便生成 PDF 文件。你能帮我解决这个问题吗?

标签: docker asp.net-core .net-core dockerfile


【解决方案1】:

知道了。修改 Dockerfile 的开头如下:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

#Add these two lines
RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig

WORKDIR /app
EXPOSE 80
[...]

第一行更新了 Linux 操作系统中的默认 /etc/apt/sources.list 文件以包含“contrib”存档区域(ttf-mscorefonts-installer 所在的位置)。这样可以确保 apt-get 可以在第二行中正常找到并安装它(连同你还需要的 fontconfig。)

作为记录,this page 建议使用“fonts-liberation”包而不是 ttf-mscorefonts-installer,您还可以在 Dockerfile 的开头使用两行不同行如下:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

#Add these two lines for fonts-liberation instead
RUN apt-get update; apt-get install -y fontconfig fonts-liberation
RUN fc-cache -f -v

WORKDIR /app
EXPOSE 80

[...]

【讨论】:

    【解决方案2】:

    您可以将自定义字体复制到 docker 图像并安装这样的字体

    RUN apt-get -y install fontconfig
    COPY /fonts ~/.fonts
    COPY /fonts /usr/shared/fonts
    COPY /fonts /usr/share/fonts/truetype
    # refresh system font cache
    RUN fc-cache -f -v
    

    或者,如果您想安装 microsoft trueType 核心字体。你可以这样做

    RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections
    RUN apt-get install -y --no-install-recommends fontconfig ttf-mscorefonts-installer
    # refresh system font cache
    RUN fc-cache -f -v
    

    你也可以使用这个示例 dockerfile DockerFile

    【讨论】:

    • 感谢回复,可惜没有安装ttf-mscorefonts-installer。您是否尝试将其与 ASP.NET Core 应用程序一起使用?你能不能给我看看适合你的 Dockerfile 吗?
    • 我还是不行。我看到您链接的 Dockerfile 正在使用 Ubuntu 容器。我不确定 ASP.NET Core 的默认容器映像是否属于这种情况。同样,您是否在 ASP.NET Core 中尝试过,它对您有用吗?
    • @hertzogth - 你有没有得到这个工作?我被困在完全相同的问题上,似乎没有任何效果
    • @Jonesie 不,我无法安装这些字体。在我的用例中,我可以通过将字体文件嵌入到我的应用程序中来避免这个问题。
    【解决方案3】:

    我通过将字体(来自我的 Windows 开发机器)添加到 api 项目下的 ./fonts 文件夹来解决这个问题。然后在 Dockerfile 我复制并配置这些:

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
    
    # not sure we need all of these
    RUN apt-get update; apt-get install -y libicu-dev libharfbuzz0b libfontconfig1 libfreetype6 fontconfig
    
    ...
    
    RUN dotnet restore "Api/Api.csproj"
    COPY . .
    WORKDIR "/src/Api"
    RUN dotnet build "Api.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "Api.csproj" -c Release  -r linux-x64 -o /app/publish
    
    FROM base AS final
    
    WORKDIR /usr/share/fonts/truetype/ms
    COPY --from=publish /app/publish/fonts .
    RUN fc-cache -f -v
    
    ...
    

    我想如果我可以使用字体而不必将内容复制到 /usr/share 会更好。

    尽管有许多网站描述了该过程,但我无法让 ttf-mscorefonts-installer 工作。

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 2017-06-07
      • 2018-12-18
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 2018-08-15
      相关资源
      最近更新 更多