How to install fonts in Docker?
Asked Answered
G

4

16

How to install fonts for all languages? This is what I do, but no Japanese fonts in Chrome.

From this image: https://github.com/Zenika/alpine-chrome/blob/master/Dockerfile

FROM zenika/alpine-chrome

USER root

RUN apk add --no-cache msttcorefonts-installer fontconfig
RUN update-ms-fonts

# Installs latest Chromium package.
RUN apk add --no-cache \
    msttcorefonts-installer fontconfig \
    font-noto \
    font-noto-adlam \
    font-noto-adlamunjoined \
    font-noto-arabic \
    font-noto-armenian \
    font-noto-avestan \
    font-noto-bamum \
    font-noto-bengali \
    font-noto-buhid \
    font-noto-carian \
    font-noto-chakma \
    font-noto-cherokee \
    font-noto-cypriot \
    font-noto-deseret \
    font-noto-devanagari \
    font-noto-ethiopic \
    font-noto-extra \
    font-noto-georgian \
    font-noto-glagolitic \
    font-noto-gothic \
    font-noto-gujarati \
    font-noto-gurmukhi \
    font-noto-hebrew \
    font-noto-kannada \
    font-noto-kayahli \
    font-noto-khmer \
    font-noto-lao \
    font-noto-lisu \
    font-noto-malayalam \
    font-noto-mandaic \
    font-noto-myanmar \
    font-noto-nko \
    font-noto-olchiki \
    font-noto-oldturkic \
    font-noto-oriya \
    font-noto-osage \
    font-noto-osmanya \
    font-noto-shavian \
    font-noto-sinhala \
    font-noto-tamil \
    font-noto-telugu \
    font-noto-thaana \
    font-noto-thai \
    font-noto-tibetan \
    font-noto-tifinagh \
    font-noto-vai \
    terminus-font \
    ttf-opensans \
    font-bakoma \
    font-misc-misc \
    font-croscore

RUN fc-cache -f && rm -rf /var/cache/*

USER chrome

Gluey answered 8/7, 2019 at 15:1 Comment(0)
G
18

My solution that worked is to download Google fonts and install it manually. Image size grows up to 1GB.

FROM zenika/alpine-chrome

USER root

RUN apk add --no-cache msttcorefonts-installer fontconfig
RUN update-ms-fonts

# Google fonts
RUN wget https://github.com/google/fonts/archive/master.tar.gz -O gf.tar.gz
RUN tar -xf gf.tar.gz
RUN mkdir -p /usr/share/fonts/truetype/google-fonts
RUN find $PWD/fonts-master/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || return 1
RUN rm -f gf.tar.gz
RUN fc-cache -f && rm -rf /var/cache/*

USER chrome
Gluey answered 11/7, 2019 at 8:48 Comment(5)
You should accept your own answer :-) Plus, you may get a nice saving by building the image with docker build --squash, so the removed files are actually purged from the docker storageReify
Works like a charm :) only the Archive Filename seems to have changed from "master" to "main". So all I had to do was change the URL to: github.com/google/fonts/archive/main.tar.gzKelseykelsi
"--squash" is only supported on a Docker daemon with experimental features enabledKarmen
One question: instead of install all the google fonts, is possible to only install one? Thank you so much!Psychoneurosis
This Dockerfile doesn't work anymore because google fonts renamed 'master' to 'main'. axellarsson.com/blog/google-fonts-docker-containerTrachoma
C
20

The URL to download the google fonts in the accepted answer has changed. The new URL should be,

https://github.com/google/fonts/archive/master.tar.gz --> https://github.com/google/fonts/archive/main.tar.gz

The new directory should be, $PWD/fonts-master/ --> $PWD/fonts-main/

Hence in the Docker file, it should be;

RUN wget https://github.com/google/fonts/archive/main.tar.gz -O gf.tar.gz
RUN tar -xf gf.tar.gz
RUN mkdir -p /usr/share/fonts/truetype/google-fonts
RUN find $PWD/fonts-main/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || return 1
RUN rm -f gf.tar.gz
RUN fc-cache -f && rm -rf /var/cache/*

I made the following changes to the above script:

  • Use less RUN operations inside Docker to limit the build steps.
  • Remove the extracted fonts directory $PWD/fonts-main which would otherwise take twice the space for google fonts.
  • Swap the commands of RUN fc-cache -f && rm -rf /var/cache/* since it did not make much sense to me to clear the application cache just after building the font cache.

What I finally came up was,

RUN wget https://github.com/google/fonts/archive/main.tar.gz -O gf.tar.gz && \
  tar -xf gf.tar.gz && \
  mkdir -p /usr/share/fonts/truetype/google-fonts && \
  find $PWD/fonts-main/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || return 1 && \
  rm -f gf.tar.gz && \
  # Remove the extracted fonts directory
  rm -rf $PWD/fonts-main && \
  # Remove the following line if you're installing more applications after this RUN command and you have errors while installing them
  rm -rf /var/cache/* && \
  fc-cache -f

Cheers 🍻 !!!

Cirrus answered 12/3, 2021 at 3:9 Comment(0)
G
18

My solution that worked is to download Google fonts and install it manually. Image size grows up to 1GB.

FROM zenika/alpine-chrome

USER root

RUN apk add --no-cache msttcorefonts-installer fontconfig
RUN update-ms-fonts

# Google fonts
RUN wget https://github.com/google/fonts/archive/master.tar.gz -O gf.tar.gz
RUN tar -xf gf.tar.gz
RUN mkdir -p /usr/share/fonts/truetype/google-fonts
RUN find $PWD/fonts-master/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || return 1
RUN rm -f gf.tar.gz
RUN fc-cache -f && rm -rf /var/cache/*

USER chrome
Gluey answered 11/7, 2019 at 8:48 Comment(5)
You should accept your own answer :-) Plus, you may get a nice saving by building the image with docker build --squash, so the removed files are actually purged from the docker storageReify
Works like a charm :) only the Archive Filename seems to have changed from "master" to "main". So all I had to do was change the URL to: github.com/google/fonts/archive/main.tar.gzKelseykelsi
"--squash" is only supported on a Docker daemon with experimental features enabledKarmen
One question: instead of install all the google fonts, is possible to only install one? Thank you so much!Psychoneurosis
This Dockerfile doesn't work anymore because google fonts renamed 'master' to 'main'. axellarsson.com/blog/google-fonts-docker-containerTrachoma
G
1

use --no-check-certificate

FROM adoptopenjdk/openjdk8:jdk8u252-b09-alpine
    MAINTAINER JhonLarru
    EXPOSE 7105
    COPY src/main/resources/reportes/ /app/
    COPY build/libs/*.jar /app/application.jar
    RUN apk add --no-cache msttcorefonts-installer fontconfig
    RUN update-ms-fonts
    # Google fonts
    RUN wget https://github.com/google/fonts/archive/master.tar.gz -O gf.tar.gz --no-check-certificate
    RUN tar -xf gf.tar.gz
    RUN mkdir -p /usr/share/fonts/truetype/google-fonts
    RUN find $PWD/fonts-master/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || return 1
    RUN rm -f gf.tar.gz
    RUN fc-cache -f && rm -rf /var/cache/*
    ENTRYPOINT ["java", "-Djava.awt.headless=true", "-Duser.timezone=America/Lima", "-Xms128m", "-Xmx128m", "-jar", "/app/application.jar", "server", "--spring.config.location=file:/config/application.yaml"]
Glob answered 27/6, 2020 at 3:51 Comment(0)
N
0

Just copy fonts from a local folder with:

RUN cp ./conf/font/*  /usr/local/share/fonts/ && dpkg-reconfigure fontconfig-config
Nilsanilsen answered 10/3, 2022 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.