Table of Contents
Docker 和中文字体
下载中文字体
安装中文字体
总结
Home Operation and Maintenance Docker How to install Chinese fonts in docker

How to install Chinese fonts in docker

Apr 19, 2023 pm 05:25 PM

近年来,容器化技术得以广泛应用并逐渐成为主流,Docker 是其中的佼佼者。它的使用和安装非常方便,但是如果你想在 Docker 运行的容器中使用中文,那么安装中文字体就必不可少了。

本文将介绍如何在 Docker 中安装中文字体,以帮助你在容器中使用中文。

Docker 和中文字体

在 Docker 中,每个容器都是独立的,基本上没有任何依赖关系。因此,如果你想在容器中使用一种新的字体,你需要在容器中安装它。这与在主机上安装字体的过程类似,只需要将字体文件复制到容器中并执行一些命令即可。

下载中文字体

首先,你需要下载所需的中文字体文件。这里以 FZSTK.TTF 为例。

虽然你可以在互联网上轻易地找到各种字体文件,但是出于版权和安全的考虑,最好在官方网站下载字体文件。比如,方正字库提供了常用的中文字体文件下载。

下载好字体文件后,将其复制到 Docker 镜像内,为此你需要使用 COPY 指令。这里,我们将字体文件复制到 /usr/share/fonts 目录下。

FROM debian:buster-slim
COPY fonts/FZSTK.TTF /usr/share/fonts/
Copy after login

安装中文字体

现在,我们已经将字体文件复制到容器中了。接下来,就需要安装这些字体了。在 Debian 或者 Ubuntu 等系统中,可以通过一些命令将字体文件注册到系统中。

安装字体文件的命令如下:

RUN apt-get update && \
    apt-get install -y fontconfig
Copy after login

然后,在容器中使用 fc-cache 命令刷新字体缓存。这个命令会扫描所有字体文件,然后注册它们。同时,它会生成一些字体缓存文件,这样系统就能快速找到并使用这些字体了。

RUN fc-cache -f -v
Copy after login

现在,中文字体已经安装好了。你可以测试它们是否可用了。为了测试字体是否可用,你可以在容器中运行一些命令。例如,你可以使用以下命令测试 simsun.ttf(宋体)是否安装成功:

RUN echo -e "\
    \n#include <stdio.h>\
    \n#include <ft2build.h>\
    \n#include FT_FREETYPE_H\
    \nint main(int argc,char **argv) {\
    \n  FT_Library library;\
    \n  FT_Face face;\
    \n  FT_Error error;\
    \n  char *fname = \"/usr/share/fonts/simsun.ttf\";\
    \n  error = FT_Init_FreeType(&library);\
    \n  error = FT_New_Face(library, fname, 0, &face);\
    \n  if (error) {\
    \n    printf(\"ERROR %d!\\n\", error);\
    \n    exit(1);\
    \n  }\
    \n  printf(\"Face family: %s\\n\", face->family_name);\
    \n  exit(0);\
    \n}\
    " > 1.c
RUN gcc 1.c -o 1 `pkg-config --cflags --libs freetype2`
RUN ./1
Copy after login

在命令行输出中,你应该能够看到如下字样:

Face family: SIMSUN
Copy after login

这就证明 simsun.ttf 字体安装成功,现在你就可以在容器中使用宋体了。

总结

在这篇文章中,我们讨论了如何在 Docker 中安装中文字体。通过将字体文件复制到容器中,执行一些命令并刷新字体缓存,我们就能够在容器中使用中文了。这些步骤不仅适用于中文字体,还可以用于其他语言和字体。希望这篇文章能对你有所帮助。

The above is the detailed content of How to install Chinese fonts in docker. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24
Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

Docker vs. Kubernetes: Key Differences and Synergies Docker vs. Kubernetes: Key Differences and Synergies May 01, 2025 am 12:09 AM

Docker and Kubernetes are leaders in containerization and orchestration. Docker focuses on container lifecycle management and is suitable for small projects; Kubernetes is good at container orchestration and is suitable for large-scale production environments. The combination of the two can improve development and deployment efficiency.

Why Use Docker? Benefits and Advantages Explained Why Use Docker? Benefits and Advantages Explained Apr 25, 2025 am 12:05 AM

The reason for using Docker is that it provides an efficient, portable and consistent environment to package, distribute, and run applications. 1) Docker is a containerized platform that allows developers to package applications and their dependencies into lightweight, portable containers. 2) It is based on Linux container technology and joint file system to ensure fast startup and efficient operation. 3) Docker supports multi-stage construction, optimizes image size and deployment speed. 4) Using Docker can simplify development and deployment processes, improve efficiency and ensure consistency across environments.

Docker: The Containerization Tool, Kubernetes: The Orchestrator Docker: The Containerization Tool, Kubernetes: The Orchestrator Apr 21, 2025 am 12:01 AM

Docker is a containerization tool, and Kubernetes is a container orchestration tool. 1. Docker packages applications and their dependencies into containers that can run in any Docker-enabled environment. 2. Kubernetes manages these containers, implementing automated deployment, scaling and management, and making applications run efficiently.

Mastering Docker: A Guide for Linux Users Mastering Docker: A Guide for Linux Users Apr 18, 2025 am 12:08 AM

Using Docker on Linux can improve development efficiency and simplify application deployment. 1) Pull Ubuntu image: dockerpullubuntu. 2) Run Ubuntu container: dockerrun-itubuntu/bin/bash. 3) Create Dockerfile containing nginx: FROMubuntu;RUNapt-getupdate&&apt-getinstall-ynginx;EXPOSE80. 4) Build the image: dockerbuild-tmy-nginx. 5) Run container: dockerrun-d-p8080:80

Kubernetes and Docker: A Comparative Analysis Kubernetes and Docker: A Comparative Analysis Apr 27, 2025 am 12:05 AM

The main difference between Docker and Kubernetes is that Docker is used for containerization, while Kubernetes is used for container orchestration. 1.Docker provides a consistent environment to develop, test and deploy applications, and implement isolation and resource limitation through containers. 2. Kubernetes manages containerized applications, provides automated deployment, expansion and management functions, and supports load balancing and automatic scaling. The combination of the two can improve application deployment and management efficiency.

Running Docker on Linux: Installation and Configuration Running Docker on Linux: Installation and Configuration Apr 26, 2025 am 12:12 AM

Installing and configuring Docker on Linux requires ensuring that the system is 64-bit and kernel version 3.10 and above, use the command "sudoapt-getupdate" and install it with the command "sudoapt-getupdate" and verify it with "sudoapt-getupdate" and. Docker uses the namespace and control groups of the Linux kernel to achieve container isolation and resource limitation. The image is a read-only template, and the container can be modified. Examples of usage include running an Nginx server and creating images with custom Dockerfiles. common

Docker and Linux: The Perfect Partnership Docker and Linux: The Perfect Partnership Apr 30, 2025 am 12:02 AM

Docker and Linux are perfect matches because they can simplify the development and deployment of applications. 1) Docker uses Linux's namespaces and cgroups to implement container isolation and resource management. 2) Docker containers are more efficient than virtual machines, have faster startup speeds, and the mirrored hierarchical structure is easy to build and distribute. 3) On Linux, the installation and use of Docker is very simple, with only a few commands. 4) Through DockerCompose, you can easily manage and deploy multi-container applications.

See all articles