Installing fonts on Docker Container running Ubuntu 18.04

I'm trying to install fonts in a docker container running Ubuntu 18.04 (Dockerfile inherits from the Jupyter scipy notebook which inherits from the base jupyter image, Dockerfile here).

I've tried lots of different things including this answer and other suggestions in there.

My Dockerfile looks like

FROM jupyter/scipy-notebook

USER root

# bash instead of dash to use source
RUN ln -snf /bin/bash /bin/sh

# These require sudo so they must come before defining
# a user

# Font attempt
COPY GillSansMTPro-Medium.otf /usr/local/share/fonts
RUN fc-cache -f -v

# installing some pip packages

When I try to use this font in matplotlib, I see this error: error message

I've tried adding

RUN rm -fr ~/.cache/matplotlib

to my Dockerfile (after the part shown above) since I read online that could solve the issue. It didn't work either.

Also, if I navigate to /usr/local/share/fonts, the font is there as expected.

Any ideas how to solve this?


I had a similar problem with using custom fonts from within a Java service running in a Docker container. Basically, there are two ways how you can set things up:

  1. Put fonts to the image when building it,
  2. Attach a volume with fonts when starting the container;

The first one is useful if you plan reusing the container for different apps that require fonts. For the latter one, you can prepare the environment once (e.g. test or prod) and then share the fonts across different docker containers (even 3rd party ones) with no need to rebuild them.

Here's an example how to prepare the env and then attach fonts ad-hoc:

$ wget https://www.paratype.ru/uni/public/PTSans.zip \
 -O ~/.fonts/PTSans.zip
$ cd ~/.fonts 
$ unzip PTSans.zip
$ sudo cp -rfv .fonts /usr/share/fonts/
$ cd /usr/share/fonts/
$ sudo mv .fonts/ pt_sans/
$ cd pt_sans
$ fc-cache -fv
$ docker run -d --name reports \
  -v /usr/share/fonts/pt_sans:/usr/share/fonts/pt_sans \
  your-container/reports

Make sure you updated it with relevant names for your case.

There are some more details described in this article.


I was in the same situation like you before.

Here is my Docker file. I hope this may help you.

FROM jupyter/scipy-notebook

# create directory for cuistom.css and copy it.
RUN mkdir -p /home/jovyan/.jupyter/custom
COPY custom.css /home/jovyan/.jupyter/custom

# create font directory and copy the font
RUN mkdir -p /home/jovyan/.fonts
COPY D2Coding.ttf /home/jovyan/.fonts
COPY D2CodingBold.ttf /home/jovyan/.fonts

# refresh system font cache
RUN fc-cache -f -v

# refresh matplotlib font cache
RUN rm -fr ~/.cache/matplotlib

It works in my case.