Docker 构建支持中文环境的Alpine及大小优化

因SpringBoot日志出现乱码,研究原因后发现是从docker官方仓库pull的镜像不支持中文。

构建内容参考了:https://www.clxz.top/2019/05/09/160241/

那就自己构建一个环境,本文只说构建Alpine及优化

1、Dockerfile如下:

FROM alpine:3.12.0

# 清理临时文件要在 同一个RUN命令内进行, rm -rf .....,构建的时候每个RUN都会创建一个临时的容器,只有写在同一个RUN下才会在一个容器内执行
RUN apk --no-cache add ca-certificates wget && \
    wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
    wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-2.25-r0.apk && \
    wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-bin-2.25-r0.apk && \
    wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-i18n-2.25-r0.apk && \
    apk add glibc-bin-2.25-r0.apk glibc-i18n-2.25-r0.apk glibc-2.25-r0.apk && \
    rm -rfv glibc-bin-2.25-r0.apk glibc-i18n-2.25-r0.apk glibc-2.25-r0.apk
# locale.md 见下面的内容
COPY ./locale.md /locale.md
RUN cat locale.md | xargs -i /usr/glibc-compat/bin/localedef -i {} -f UTF-8 {}.UTF-8 && \
    rm -rfv locale.md

ENV LANG=en_US.UTF-8 \
    LANGUAGE=en_US.UTF-8

精简后的locale.md,  只保留了en 、zh开头的, 不然构建的镜像比较大,将近大了80Mb

en_AG
en_AU
en_BW
en_CA
en_DK
en_GB
en_HK
en_IE
en_IN
en_NG
en_NZ
en_PH
en_SG
en_US
en_ZA
en_ZM
en_ZW
zh_CN
zh_HK
zh_SG
zh_TW
zu_ZA

清理临时文件内容片段:

RUN apk --no-cache add ca-certificate。。。 \
   。。。。
    rm -rfv glibc-bin-2.25-r0.apk glibc-i18n-2.25-r0.apk glibc-2.25-r0.apk

RUN cat locale.md | xargs -i 。。。&& \
    rm -rfv locale.md

2、构建命令:

docker build -t my-env/utf8-alpine:20.7.22.3 .

3、构建的过程:

Sending build context to Docker daemon  124.9MB
Step 1/5 : FROM alpine:3.12.0
---> a24bb4013296
Step 2/5 : RUN apk --no-cache add ca-certificates wget &&     wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub &&     wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-2.25-r0.apk &&     wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-bin-2.25-r0.apk &&     wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-i18n-2.25-r0.apk &&     apk add glibc-bin-2.25-r0.apk glibc-i18n-2.25-r0.apk glibc-2.25-r0.apk &&     rm -rfv glibc-bin-2.25-r0.apk glibc-i18n-2.25-r0.apk glibc-2.25-r0.apk
---> Running in 27530d5a8b80
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/4) Installing ca-certificates (20191127-r4)
(2/4) Installing libunistring (0.9.10-r0)
(3/4) Installing libidn2 (2.3.0-r0)
(4/4) Installing wget (1.20.3-r1)
Executing busybox-1.31.1-r16.trigger
Executing ca-certificates-20191127-r4.trigger
OK: 8 MiB in 18 packages
--2020-07-22 09:11:56--  htt 。。。。
。。。。。 下载文件

fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/4) Installing glibc (2.25-r0)
(2/4) Installing libgcc (9.3.0-r2)
(3/4) Installing glibc-bin (2.25-r0)
(4/4) Installing glibc-i18n (2.25-r0)
Executing glibc-bin-2.25-r0.trigger
OK: 25 MiB in 22 packages
removed 'glibc-bin-2.25-r0.apk'
removed 'glibc-i18n-2.25-r0.apk'
removed 'glibc-2.25-r0.apk'
Removing intermediate container 27530d5a8b80
---> 9927c6d3def0
Step 3/5 : COPY ./locale.md /locale.md
---> 37cc6234b3ab
Step 4/5 : RUN cat locale.md | xargs -i /usr/glibc-compat/bin/localedef -i {} -f UTF-8 {}.UTF-8
---> Running in 943755a1b6e2
Removing intermediate container 943755a1b6e2
---> c08ec601f5e2
Step 5/5 : ENV LANG=en_US.UTF-8     LANGUAGE=en_US.UTF-8
---> Running in 1983050bcf0a
Removing intermediate container 1983050bcf0a
---> 58488cb2f47b
Successfully built 58488cb2f47b
Successfully tagged my-env/utf8-alpine:20.7.22.2

4、查看构建后的文件结构

// 导出images
docker save my-env/utf8-alpine:20.7.22.3 > utf8-alpine_20.7.22.3.tar

解压后:
-
    - manifest.json
    - repositories
  - 99562d84c6129cf4268373af341e110e8984e12626951c19515f956da947ccc6.json
  - e56500c70e5e3b9b23b2e31880fdcb10ec2eb8b262609f8f0463bfa61916fd01     // 这个是alpine:3.12.0 的layer
    - layer.tar
    - json
    - VERSION
  - c234ab8959edd9715318124ad66fe080ea55c2d95a77e98613e2e98411874d96    // 安装glibc-2.25xxx 的 layer
    - layer.tar
    - json
    - VERSION
  - f3f134a63b698e4faf269116d0c3f12c76b6bee939fea010eceacd4d5404a00f    // locale.md 的layer
    - layer.tar
    - json
    - VERSION
  - 034a7ec19dfd3ab16e3f56e02bd4b9d816d390feee1da12194cacb1d546af577    // 根据local.md 生成的 locale 信息
    - layer.tar
    - json
    - VERSION

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐