Search code examples
dockerasp.net-core.net-coredockerfile

Install fonts in Linux container for ASP.NET Core


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?


Solution

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