Bootstrap

docker中centos7|8和alpine 中文 乱码问题 终结者,制作 docker 精简 jre8 基础镜像alpine

一:默认的centos镜像中locale 显示的是POSIX 这种是不支持中文的

[root@localhost jettoloader]# docker run -it -d harbor.jettech.com/jettechtools/centos:7.9.2009 bash
bb9987d77c1bace9696c046c91dd649d84a27be468d7154bc3ce5c02776cd04d

[root@localhost jettoloader]# docker exec -it bb  bash
[root@f55d4fa11d5c work]# locale
LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
[root@f55d4fa11d5c work]# cat /etc/locale.conf 
LANG="en_US.UTF-8"
[root@f55d4fa11d5c work]# ls /usr/share/locale/zh_CN/       
[root@f55d4fa11d5c work]# localedef --list-archive
en_AG
en_AG.utf8
en_AU
en_AU.iso88591
en_AU.utf8
en_BW
en_BW.iso88591
en_BW.utf8
en_CA
en_CA.iso88591
en_CA.utf8
en_DK
en_DK.iso88591
en_DK.utf8
en_GB
en_GB.iso88591
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.iso88591
en_HK.utf8
en_IE
en_IE.iso88591
en_IE.iso885915@euro
en_IE.utf8
en_IE@euro
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ
en_NZ.iso88591
en_NZ.utf8
en_PH
en_PH.iso88591
en_PH.utf8
en_SG
en_SG.iso88591
en_SG.utf8
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
en_ZA
en_ZA.iso88591
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW
en_ZW.iso88591
en_ZW.utf8

[root@f55d4fa11d5c work]# rpm -qa | grep glibc-common
glibc-common-2.17-317.el7.x86_64
[root@f55d4fa11d5c work]# rpm -qa | grep kde-l10n-Chinese

二,操作支持

2.1 直接编写看下能否改变成识别中文字体,

      写到你的~/.bashrc里吧,然后重启终端

[root@f55d4fa11d5c work]# cat  ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
export LC_ALL=zh_CN.UTF-8
export LANGUAGE=zh_CN.UTF-8
export LANG=zh_CN.UTF-8

[root@f55d4fa11d5c work]# source  ~/.bashrc 
bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8): No such file or directory
bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)


[root@f55d4fa11d5c work]# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=zh_CN.UTF-8

看效果此 

[root@f55d4fa11d5c work]# vi a

 执行一下,转化语言环境和字符集

[root@ef3d25d5714d work]# localedef -c -f UTF-8 -i zh_CN zh_CN.utf8

[root@ef3d25d5714d work]# localedef --list-archive
en_AG
en_AG.utf8
en_AU
en_AU.iso88591
en_AU.utf8
en_BW
en_BW.iso88591
en_BW.utf8
en_CA
en_CA.iso88591
en_CA.utf8
en_DK
en_DK.iso88591
en_DK.utf8
en_GB
en_GB.iso88591
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.iso88591
en_HK.utf8
en_IE
en_IE.iso88591
en_IE.iso885915@euro
en_IE.utf8
en_IE@euro
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ
en_NZ.iso88591
en_NZ.utf8
en_PH
en_PH.iso88591
en_PH.utf8
en_SG
en_SG.iso88591
en_SG.utf8
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
en_ZA
en_ZA.iso88591
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW
en_ZW.iso88591
en_ZW.utf8
zh_CN.utf8
[root@ef3d25d5714d work]# locale
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=zh_CN.UTF-8

在测试:

[root@f55d4fa11d5c work]# vi a

方法二:进入docker里配置

#添加中文环境编码,安装两个包 如果有的话就不用装了 我的系统默认有glibc-common 就可以了不用安装kde-l10n-Chinese 
[root@d547146f3672 work]# yum install kde-l10n-Chinese  glibc-common -y

#转化语言环境和字符集 这个是主要的
[root@d547146f3672 work]# localedef -c -f UTF-8 -i zh_CN zh_CN.utf8

#添加定义到系统环境变量
[root@d547146f3672 work]# echo "export LC_ALL=zh_CN.utf8" >> /etc/profile

执行生效
[root@d547146f3672 work]# source /etc/profile

[root@d547146f3672 work]# localedef --list-archive
en_US.utf8
zh_CN.utf8
[root@d547146f3672 work]# locale
LANG=
LC_CTYPE="zh_CN.utf8"
LC_NUMERIC="zh_CN.utf8"
LC_TIME="zh_CN.utf8"
LC_COLLATE="zh_CN.utf8"
LC_MONETARY="zh_CN.utf8"
LC_MESSAGES="zh_CN.utf8"
LC_PAPER="zh_CN.utf8"
LC_NAME="zh_CN.utf8"
LC_ADDRESS="zh_CN.utf8"
LC_TELEPHONE="zh_CN.utf8"
LC_MEASUREMENT="zh_CN.utf8"
LC_IDENTIFICATION="zh_CN.utf8"
LC_ALL=zh_CN.utf8

测试: 

[root@d547146f3672 work]# vi a

 方法三:编写dockerfile文件

[root@localhost base]# cat Dockerfile 
FROM harbor.jettech.com/jettechtools/centos:7.9.2009
MAINTAINER [email protected]
RUN yum install -y wget  net-tools iproute bind-utils lrzsz \
    && rm -rf /var/cache /tmp/* \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8

#RUN yum install kde-l10n-Chinese -y
#RUN yum install glibc-common -y
#RUN export LANG=zh_CN.UTF-8
#RUN echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf
#ENV LANG zh_CN.UTF-8
ADD jre-8u321-linux-x64-minimal.tar.gz /usr/java/jdk
ENV LC_ALL zh_CN.UTF-8
ENV JAVA_HOME /usr/java/jdk
ENV PATH ${PATH}:${JAVA_HOME}/bin
WORKDIR /jettech/work

制作 docker 精简 jre8 基础镜像

官方jre下载地址:https://www.java.com/en/download/manual.jsp

然后解压JRE包,删除根目录下文本文件,然后删除其他不必要文件,命令如下:

# 解压
[root@localhost base]#tar -zxvf jre-8u321-linux-x64.tar.gz
# 进入目录
[root@localhost base]#cd jre1.8.0_321/
# 删除文本文件
[root@localhost base]#rm -rf COPYRIGHT LICENSE README release THIRDPARTYLICENSEREADME-JAVAFX.txt THIRDPARTYLICENSEREADME.txt Welcome.html
#删除其他无用文件
[root@localhost base]# rm -rf  lib/plugin.jar \
           lib/ext/jfxrt.jar \
           bin/javaws \
           lib/javaws.jar \
           lib/desktop \
           plugin \
           lib/deploy* \
           lib/*javafx* \
           lib/*jfx* \
           lib/amd64/libdecora_sse.so \
           lib/amd64/libprism_*.so \
           lib/amd64/libfxplugins.so \
           lib/amd64/libglass.so \
           lib/amd64/libgstreamer-lite.so \
           lib/amd64/libjavafx*.so \
           lib/amd64/libjfx*.so

重新打包

# 打包,包名增加 -minimal 和原始包名进行区分
[root@localhost base]# tar -zcvf jre-8u321-linux-x64-minimal.tar.gz *

构建镜像

[root@localhost base]# cp jre-8u321-linux-x64-minimal.tar.gz  work/
[root@localhost base]# docker build -t  harbor.jettech.com/jettechtools/centos-jre-8:7.9.2009  -f Dockerfile  work/

==================

Centos8

[root@localhost base]# cat /etc/locale.conf 
LANG=en_US.utf8
[root@localhost base]# . /etc/locale.conf 
[root@localhost base]locale
LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

查看系统中已有支持的语言

[root@localhost base]# locale -a
C
C.utf8
en_AG
en_AU
en_AU.utf8
en_BW
en_BW.utf8
en_CA
en_CA.utf8
en_DK
en_DK.utf8
en_GB
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.utf8
en_IE
en_IE@euro
en_IE.utf8
en_IL
en_IN
en_NG
.
.
.
.

如果没有中文zh_CN

安装中文语言支持扩展包

[root@localhost base]# yum -y install langpacks-zh_CN

再次查看就有中文了

使用localectl命令设定系统语言为中文

查看localectl是否存在

[root@localhost base]# type localectl
localectl is hashed (/usr/bin/localectl)

输入localectl命令并用tab键补全,查看可以跟的参数

[root@localhost base]# localectl 
list-keymaps              list-x11-keymap-options   set-x11-keymap
list-locales              list-x11-keymap-variants  status
list-x11-keymap-layouts   set-keymap                
list-x11-keymap-models    set-locale                

选择set-locale参数 ,tab补全 ,使用LANG=参数查看当前系统支持的语言,相当于locale -a。

[root@localhost base]# localectl set-locale LANG=
C.utf8           en_GB.utf8       en_PH.utf8       zh_CN
en_AG            en_HK            en_SC.utf8       zh_CN.gb18030
en_AU            en_HK.utf8       en_SG            zh_CN.gbk
en_AU.utf8       en_IE            en_SG.utf8       zh_CN.utf8
en_BW            en_IE@euro       en_US            zh_HK
en_BW.utf8       en_IE.utf8       en_US.iso885915  zh_HK.utf8
en_CA            en_IL            en_US.utf8       zh_SG
en_CA.utf8       en_IN            en_ZA            zh_SG.gbk
en_DK            en_NG            en_ZA.utf8       zh_SG.utf8
en_DK.utf8       en_NZ            en_ZM            zh_TW
en_GB            en_NZ.utf8       en_ZW            zh_TW.euctw
en_GB.iso885915  en_PH            en_ZW.utf8       zh_TW.utf8

设定系统语言为zh_CN.utf8

[root@C8-3 ~]# localectl set-locale LANG=zh_CN.utf8

查看当前系统语言

[root@localhost base]# locale
LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

发现并没有生效!

因为重启才会重新加载配置文件

不重启马上生效的办法

手动加载配置文件,使配置文件被加载到内存中去

[root@localhost base]# . /etc/locale.conf 
[root@localhost base]# locale
LANG=zh_CN.utf8
LC_CTYPE="zh_CN.utf8"
LC_NUMERIC="zh_CN.utf8"
LC_TIME="zh_CN.utf8"
LC_COLLATE="zh_CN.utf8"
LC_MONETARY="zh_CN.utf8"
LC_MESSAGES="zh_CN.utf8"
LC_PAPER="zh_CN.utf8"
LC_NAME="zh_CN.utf8"
LC_ADDRESS="zh_CN.utf8"
LC_TELEPHONE="zh_CN.utf8"
LC_MEASUREMENT="zh_CN.utf8"
LC_IDENTIFICATION="zh_CN.utf8"
LC_ALL=

Docker Hub
alpine:3.10

FROM harbor.jettech.com/jettechtools/alpine:3.10
MAINTAINER [email protected]

WORKDIR /opt/work
#RUN apk add -y --no-cache   wget  net-tools iproute  busybox-extras\
#    && rm -rf /var/cache /tmp/* \
#    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
#    && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
#RUN export LANG=zh_CN.UTF-8
#RUN echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf
#ENV LANG zh_CN.UTF-8
COPY sgerrand.rsa.pub   sgerrand.rsa.pub
COPY glibc-2.29-r0.apk  glibc-2.29-r0.apk
COPY glibc-bin-2.29-r0.apk glibc-bin-2.29-r0.apk
COPY glibc-dev-2.29-r0.apk glibc-dev-2.29-r0.apk
COPY glibc-i18n-2.29-r0.apk glibc-i18n-2.29-r0.apk
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
    echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories && \
    apk update && apk upgrade && \
    cp sgerrand.rsa.pub /etc/apk/keys/sgerrand.rsa.pub && \
    apk add --no-cache tzdata *.apk busybox-extras curl ttf-dejavu fontconfig vim && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    /usr/glibc-compat/bin/localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 && \
    #mkfontscale && mkfontdir && fc-cache \ 
    #fc-list && \
    echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf &&\
    echo "Asia/Shanghai" > /etc/timezone && \
    apk del tzdata && \
    rm -rf /var/cache/apk/* *.apk sgerrand.rsa.pub

ADD jre-8u321-linux-x64-minimal.tar.gz /usr/java/jre
ENV LC_ALL zh_CN.UTF-8
ENV LANG zh_CN.UTF-8
ENV JAVA_HOME /usr/java/jre
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH ${PATH}:${JAVA_HOME}/bin

资源文件:https://download.csdn.net/download/Michaelwubo/88244999

https://github.com/CentOS/sig-cloud-instance-images/tree/CentOS-7-x86_64/docker

https://github.com/CentOS/sig-cloud-instance-build

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;