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中的不同字体,汉字一般没有差别,只是英文符号的宽度不一样,以便适应不同的版面要求
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

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