Docker 在ASP.NET Core的Linux容器中安装字体

Docker 在ASP.NET Core的Linux容器中安装字体,docker,asp.net-core,.net-core,dockerfile,Docker,Asp.net Core,.net Core,Dockerfile,在Visual Studio中,我创建了一个默认的ASP.NET核心Web应用程序,并启用了Docker支持。 它使用默认的Linux容器 这是我的Dockerfile: 我想在其上安装Microsoft Windows字体,我尝试了以下操作,但无效: RUN apt install ttf-mscorefonts-installer 如何在此容器上安装字体?您可以将自定义字体复制到docker图像,并安装如下字体 RUN apt-get -y install fontconfig COPY

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

这是我的Dockerfile:

我想在其上安装Microsoft Windows字体,我尝试了以下操作,但无效:

RUN apt install ttf-mscorefonts-installer

如何在此容器上安装字体?

您可以将自定义字体复制到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
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
或者如果要安装microsoft trueType core字体。你可以这样做

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

我通过将字体(从我的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安装程序正常工作。

明白了。修改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
[...]
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

[...]
第一行更新Linux操作系统中的默认/etc/apt/sources.list文件,以包括“contrib”存档区域(它是存在的地方)。这确保了apt get可以在第二行中找到并正常安装它(以及fontconfig,您也需要它)

作为记录,建议使用“字体解放”软件包而不是ttf mscorefonts安装程序,您也可以在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
[...]
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

[...]

为什么需要安装字体?对于web应用程序,使用的字体取决于客户端计算机上安装的字体,而不是服务器上安装的字体。@ChrisPratt我需要这些字体,以便生成PDF文件。你能帮我吗?谢谢你的回复,但不幸的是它没有安装ttf mscorefonts安装程序。您是否尝试在ASP.NET核心应用程序中使用此功能?你能给我看一下为你工作的Dockerfile吗?我还是不工作。我看到你链接的Dockerfile正在使用Ubuntu容器。我不确定ASP.NET Core的默认容器映像是否如此。再说一次,你用ASP.NET内核试过了吗?它对你有用吗?@hertzogth-你用过吗?我被困在同一个问题上,似乎什么也没有解决work@Jonesie不,我无法安装这些字体。在我的用例中,通过将字体文件嵌入到我的应用程序中,我能够避免这个问题。