在运行 Ubuntu 18.04 的 Docker 容器上安装字体

mca*_*ado 5 fonts docker 18.04

我正在尝试在运行 Ubuntu 18.04 的 docker 容器中安装字体(Dockerfile 继承自 Jupyter scipy 笔记本,该笔记本继承自基本 jupyter 映像,此处为Dockerfile )。

我尝试了很多不同的东西,包括这个答案和那里的其他建议。

我的 Dockerfile 看起来像

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
Run Code Online (Sandbox Code Playgroud)

当我尝试在 matplotlib 中使用这种字体时,我看到了这个错误: 错误信息

我试过添加

RUN rm -fr ~/.cache/matplotlib
Run Code Online (Sandbox Code Playgroud)

到我的 Dockerfile(在上面显示的部分之后),因为我在网上阅读可以解决问题。它也没有用。

此外,如果我导航到/usr/local/share/fonts,字体会按预期出现。

任何想法如何解决这个问题?

小智 4

我在 Docker 容器中运行的 Java 服务中使用自定义字体时遇到了类似的问题。基本上,有两种设置方法:

  1. 构建图像时将字体添加到图像中,
  2. 启动容器时附加一个带有字体的卷;

如果您计划为需要字体的不同应用程序重复使用容器,第一个很有用。对于后一种,您可以准备一次环境(例如测试或生产),然后在不同的 docker 容器(甚至是第 3 方容器)之间共享字体,而无需重建它们。

以下是如何准备环境然后附加临时字体的示例:

$ 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
Run Code Online (Sandbox Code Playgroud)

确保使用与您的案例相关的名称进行更新。

本文描述了更多细节。


小智 3

我之前也遇到过和你一样的情况。

这是我的 Docker 文件。我希望这可以帮助你。

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
Run Code Online (Sandbox Code Playgroud)

它适用于我的情况。