Install fonts in Linux container for ASP.NET Core

Viewed 10915

From Visual Studio, I've created a default ASP.NET Core Web Application with enabled Docker support.
It's using the default Microsoft Offical image for Linux container.

Here is my 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"]

I want to install Microsoft Windows Fonts on it, I tried the following but it's not working:

RUN apt install ttf-mscorefonts-installer

How can I install fonts on this container?

3 Answers

Got it. Revise the start of your Dockerfile as follows:

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
[...]

The first line updates the default /etc/apt/sources.list file in the Linux OS to include the 'contrib' archive area (which is where ttf-mscorefonts-installer lives). That ensures apt-get can find it and install it as normal in the second line (along with fontconfig, which you'll also need.)

For the record, this page suggested using the "fonts-liberation" package instead of ttf-mscorefonts-installer, which you can also get working with two different lines at the start of the Dockerfile as follows:

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

[...]

you can copy your custom fonts to the docker images and install fonts like this

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

or if you want to install microsoft trueType core fonts. you can do like this

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

you can use this sample dockerfile too DockerFile

I solved this by adding the fonts (from my Windows dev machine) to a ./fonts folder under the api project. Then in Dockerfile I copy and configure these thusly:

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

...

I guess it would be better if I could use the fonts without having to copy thing to /usr/share.

Despite many web sites describing the process I couldn't get ttf-mscorefonts-installer to work.

Related