docker容器安装宋体

前言

近期在项目开发中,遇到一个问题。程序在mac本地运行正常,弄到服务器上,运行在docker容器中异常。经过检查,问题的原因是程序使用了字体在docker中缺失。经过一番搜索,解决了这个问题,特此记录下。

原因

字体通常存在版权问题。linux上字体比较少。中文字体更是基本不会安装。

这里我使用的容器是tomcat:9-jre8-alpine

该镜像没有我需要的宋体。因此下面我来介绍下如何安装宋体。

解决

  • 查看已经安装的中文字体。
bash-4.4# fc-list :lang=zh
/usr/share/fonts/SimSun.ttf: SimSun,宋体:style=Regular
bash-4.4#

上面的表示已经安装过宋体。

  • 安装字体软件
    如果上一步操作提示该命令不存在,则说明当前系统没有安装字体软件,通常执行下面的命令进行安装
yum -y install fontconfig

由于这里我的容器是alpine,它的字体软件使用下面的命令。

apk add ttf-dejavu
  • 查看字体

查看所有字体。

bash-4.4# fc-list
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSerif-BoldItalic.ttf: DejaVu Serif:style=Bold Italic
/usr/share/fonts/SimSun.ttf: SimSun,宋体:style=Regular
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed-Italic.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed Italic,Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed-Oblique.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed-Bold.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono-BoldOblique.ttf: DejaVu LGC Sans Mono:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono-Oblique.ttf: DejaVu Sans Mono:style=Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans.ttf: DejaVu LGC Sans:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono-Bold.ttf: DejaVu Sans Mono:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono.ttf: DejaVu LGC Sans Mono:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed-BoldItalic.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed Bold Italic,Bold Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-ExtraLight.ttf: DejaVu LGC Sans,DejaVu LGC Sans Light:style=ExtraLight
/usr/share/fonts/ttf-dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed-Italic.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed Italic,Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif.ttf: DejaVu LGC Serif:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-BoldOblique.ttf: DejaVu LGC Sans:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif-BoldItalic.ttf: DejaVu LGC Serif:style=Bold Italic
/usr/share/fonts/ttf-dejavu/DejaVuSerif-Italic.ttf: DejaVu Serif:style=Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed-BoldOblique.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuMathTeXGyre.ttf: DejaVu Math TeX Gyre:style=Regular
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif-Italic.ttf: DejaVu LGC Serif:style=Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-Oblique.ttf: DejaVu LGC Sans:style=Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-Bold.ttf: DejaVu LGC Sans:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono-Bold.ttf: DejaVu LGC Sans Mono:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed-Bold.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif-Bold.ttf: DejaVu LGC Serif:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono-BoldOblique.ttf: DejaVu Sans Mono:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed-Bold.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed-BoldItalic.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed Bold Italic,Bold Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono-Oblique.ttf: DejaVu LGC Sans Mono:style=Oblique
  • 查看字体目录。
/usr/share/fonts
  • 下载我们需要的字体文件放入到字体目录中即可。
    通常的字体我们可以去windows电脑上拷贝。


    image.png

也可以选择去网络上下载。

宋体下载

下载后放入字体目录即可。
字体通常存在以ttf和ttc为后缀的2个文件,只需要其中一个即可。

ls /usr/share/fonts -lsh
total 10264
 10256 -rw-r--r--    1 root     root       10.0M Oct 28  1998 SimSun.ttf
     4 drwxr-xr-x    3 root     root        4.0K Dec  5 02:51 encodings
     4 drwxr-xr-x    2 root     root        4.0K Dec  5 02:51 ttf-dejavu
  • 启用字体

重启容器即可启用新字体。
也可以执行下面的命令,启用新字体。

fc-cache

再次执行即可看到字体已经安装。

bash-4.4# fc-list :lang=zh
/usr/share/fonts/SimSun.ttf: SimSun,宋体:style=Regular
bash-4.4#

提示

  • ttf和ttc的异同
    下载的字体一般是ttc或ttf格式的,系统显示这都是TrueType类型的字体。ttf格式的字体可以正常使用,但ttc的字体只有一些常用的汉字。
    TTC是几个TTF合成的字库,安装后字体列表中会看到两个以上的字体。两个字体中大部分字都一样时,可以将两种字体做成一个TTC文件,现在常见的TTC中的不同字体,汉字一般没有差别,只是英文符号的宽度不一样,以便适应不同的版面要求
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • https://hijiangtao.github.io/2018/04/17/Docker-in-Action/...
    旭娃阅读 2,671评论 2 14
  • 想要改进这个备忘单吗?参见[贡献](#贡献)部分! 目录 [为何选择Docker](#why-docker) [先...
    iOSDevLog阅读 2,147评论 0 3
  • 听说习惯很可怕,特别是感情养成的习惯,以前的我并不那么觉得,我对自己的认知足以让我能够控制情绪感情及行为。 但在4...
    Ermao阅读 311评论 1 1
  • 我放佛爱上了这本书,包括直子、包括渡边君,甚至死去的木月。 直子是真的以书中赤裸的献身这样的方式对渡...
    撒野喵阅读 816评论 8 1