當前位置: 妍妍網 > 碼農

SpringBoot 容器映像更新只要200k,你敢信???

2024-05-14碼農

來源:juejin.cn/post/7268183236739383337

👉 歡迎 ,你將獲得: 專屬的計畫實戰 / Java 學習路線 / 一對一提問 / 學習打卡 / 每月贈書

新計畫: 仿小紅書 (微服務架構)正在更新中... , 全棧前後端分離部落格計畫 2.0 版本完結啦, 演示連結 http://116.62.199.48/ 。全程手摸手,後端 + 前端全棧開發,從 0 到 1 講解每個功能點開發步驟,1v1 答疑,直到計畫上線。 目前已更新了261小節,累計41w+字,講解圖:1806張,還在持續爆肝中.. 後續還會上新更多計畫,目標是將Java領域典型的計畫都整一波,如秒殺系統, 線上商城, IM即時通訊,Spring Cloud Alibaba 等等,

  • 導讀

  • 基礎環境

  • 基於傳統單個jar包構建模式

  • 基礎Dockerfile

  • Base映像資訊

  • Base映像大小 622MB

  • Base映像history資訊

  • 傳統構建方式

  • [構建後映像大小 為基礎映像大小 622MB + 2*65M <= 757MB

  • 成品構建history資訊

  • 最佳化Dockerfile

  • 基於spring boot 分層構建方式

  • 探索基於分層構建原理

  • 基於分層構建構建制品映像

  • 驗證模擬開發推播程式碼更新映像

  • 驗證映像

  • 導讀

    在容器化實踐中部署spring boot套用普遍采用基礎java映像再添加jar包層來構建套用映像制品。隨著公司業務大量上雲,每個變更日上傳到雲上映像日漸增多,導致本地機房到雲上頻寬壓力倍增。為解決映像上傳問題,調研了相關技術棧決定采用spring boot 2.3新增的映像分層構建功能。

    基礎環境

  • spring boot version >= 2.3

  • java oracle jdk 8u201

  • docker version 20.10.13

  • 基於傳統單個jar包構建模式

    基礎Dockerfile

    以下是我們基於基礎映像構建spring boot映像制品的Dockerfile,采用了比較傳統的COPY jar包。相信這也是大家普遍采用的模式。Dockefile如下,該Dockerfile存在一個坑,會在下邊闡述。

    FROM registry.xxx.com/base/oracle-jdk:8u201
    ENV TZ=Asia/Shanghai
    ENV LC_ALL en_US.utf8
    WORKDIR /app
    ADD ./xxx-1.0.0.jar /app/lib/xxx-1.0.0.jar
    ADD ./entrypoint.sh /app/bin/entrypoint.sh
    ENTRYPOINT exec bash /app/bin/entrypoint.sh
    RUN ln -s /app/logs /app/log && chown 1001.1001 -R /app

    Base映像資訊

    我們base映像主要分三層添加了相關常用的工具如telnet netstat等以及安全修復

    docker image inspect registry.xxx.com/base/oracle-jdk:8u201

    分層資訊:

    "RootFS": {
    "Type""layers",
    "Layers": [
    "sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02",
    "sha256:2105884a1756425b2188084cc1738d6a6b13293773af56f6727af1be979858ed",
    "sha256:c33745b350978d855171c996779055195b09e227844b7a03a1d795aee803dbb9"
    ]
    }

    Base映像大小 622MB

    映像大小 622MB 不算太小,對比友商有些基礎映像竟然1-2G😂 簡直有點離譜。

    registry.xxx.com/base/oracle-jdk 8u201 1869b73a8999 2 years ago 622MB

    Base映像history資訊

    從構建歷史可以看出占用空間主要分為三層分別是

  • centos 7 基礎映像層

  • yum install 層

  • jdk 層

  • docker image history registry.xxx.com/base/oracle-jdk:8u201
    IMAGE CREATED CREATED BY SIZE COMMENT
    1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=… 0B
    <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB

    傳統構建方式

    docker build -t registry.xxx.com/layer/build:old .
    Sending build context to Docker daemon 67.45MB
    Step 1/8 : FROM registry.xxx.com/base/oracle-jdk:8u201
     ---> 1869b73a8999
    Step 2/8 : ENV TZ=Asia/Shanghai
     ---> Running in a67423d7246b
    Removing intermediate container a67423d7246b
     ---> 9304102c278e
    Step 3/8 : ENV LC_ALL en_US.utf8
     ---> Running in 05bf14c10efd
    Removing intermediate container 05bf14c10efd
     ---> 63fb6c823893
    Step 4/8 : WORKDIR /app
     ---> Running in 095764328d18
    Removing intermediate container 095764328d18
     ---> c057ab4a7d18
    Step 5/8 : ADD ./xxx-1.0.0.jar /app/lib/xxx-1.0.0.jar
     ---> 08928447fafa
    Step 6/8 : ADD ./entrypoint.sh /app/bin/entrypoint.sh
     ---> 0902efa62157
    Step 7/8 : ENTRYPOINT exec bash /app/bin/entrypoint.sh
     ---> Running in dc0eb814cfff
    Removing intermediate container dc0eb814cfff
     ---> 20679a170252
    Step 8/8 : RUN ln -s /app/logs /app/log && chown 1001.1001 -R /app
     ---> Running in 34159dca7464
    Removing intermediate container 34159dca7464
     ---> b6043465c072
    Successfully built b6043465c072
    Successfully tagged registry.xxx.com/layer/build:old

    構建後映像大小 為基礎映像大小 622MB + 2*65M <= 757MB

    構建後比我們預期的 jar包加基礎映像所占用的空間大😢 why?

    #jar包大小
    ls -lh xxx-1.0.0.jar
    -rw------- 1 root root 65M Aug 16 10:33 xxx-1.0.0.jar
    #映像大小
    docker image ls |grep registry.xxx.com/layer/build
    registry.xxx.com/layer/build old b6043465c072 59 seconds ago 757MB

    成品構建history資訊

    檢視構建成品的映像層發現竟然有兩層一樣大小,也會被推播了兩遍。🥲離大譜。透過分析Dockerfile是因為 RUN ln -s /app/logs /app/log && chown 1001.1001 -R /app 導致的。為保證映像安全,生產環境執行統一采用了1001帳號執行。為了保證1001對workdir有絕對的讀寫許可權。對目錄遞迴授權導致檔內容發生改變。

    在構建時認為檔發生變化產生一樣大小的兩層導致的。(這還是我自己當時最佳化引入的屎山😊)具體原因我也不太清楚docker構建時如何判定檔發生變化,我原本以為是基於MD5碼。實際上在對檔屬主屬組改變時MD5碼是不對變的。有知道的大神可以在評論區留言交流。

    docker image history registry.xxx.com/layer/build:old
    IMAGE CREATED CREATED BY SIZE COMMENT
    b6043465c072 6 minutes ago /bin/sh -c ln -s /app/logs /app/log && chown… 67.4MB
    20679a170252 6 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/sh" "-c… 0B
    0902efa62157 6 minutes ago /bin/sh -c #(nop) ADD file:9ac19caf793524884… 128B
    08928447fafa 6 minutes ago /bin/sh -c #(nop) ADD file:c7624c195e7c047ee… 67.4MB
    c057ab4a7d18 6 minutes ago /bin/sh -c #(nop) WORKDIR /app 0B
    63fb6c823893 6 minutes ago /bin/sh -c #(nop) ENV LC_ALL=en_US.utf8 0B
    9304102c278e 6 minutes ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai 0B
    1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=currycan… 0B
    <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
    #映像也推播了兩層一樣大小
    [root@localhost old-nochown]# docker push registry.xxx.com/layer/build:old
    The push refers to repository [registry.xxx.com/layer/build]
    bb35157fcb0b: Pushing [=============> ] 18.35MB/67.45MB
    3c0641004863: Pushed
    6ed0abaff975: Pushing [=====================> ] 28.38MB/67.45MB
    aa0794231b2c: Pushed
    c33745b35097: Layer already exists
    2105884a1756: Layer already exists
    174f56854903: Layer already exists

    最佳化Dockerfile

    原因知道了。最佳化思路是先建立目錄並遞迴授權。同時構建出來的jar的許可權可以透過COPY 指定1001屬主屬組。

    FROM registry.xxx.com/base/oracle-jdk:8u201
    ENV TZ=Asia/Shanghai
    ENV LC_ALL en_US.utf8
    RUN mkdir -pv /app/{bin,lib,logs} && ln -s /app/logs /app/log && chown 1001:1001 -R /app
    WORKDIR /app
    COPY --chown=1001:1001 ./xxx-1.0.0.jar /app/lib/xxx-1.0.0.jar
    COPY --chown=1001:1001 ./entrypoint.sh /app/bin/entrypoint.sh
    ENTRYPOINT exec bash /app/bin/entrypoint.sh

    最佳化後的映像只有COPY這一層的jar包了。

    docker image history registry.xxx.com/layer/build:old-mkdir
    IMAGE CREATED CREATED BY SIZE COMMENT
    b9fc3a8a97f2 29 hours ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/sh" "-c… 0B
    c823448f9582 29 hours ago /bin/sh -c #(nop) COPY --chown=1001:1001file… 128B
    9a997f3962bf 29 hours ago /bin/sh -c #(nop) COPY --chown=1001:1001file… 67.4MB
    f272c4e64f87 29 hours ago /bin/sh -c #(nop) WORKDIR /app 0B
    1a526d3df7c2 29 hours ago /bin/sh -c mkdir -pv /app/{bin,lib,logs} && … 9B
    63fb6c823893 30 hours ago /bin/sh -c #(nop) ENV LC_ALL=en_US.utf8 0B
    9304102c278e 30 hours ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai 0B
    1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=currycan… 0B
    <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB

    基於spring boot 分層構建方式

    探索基於分層構建原理

    直接上手官方文件的demo 把官方文件demo的多階段構建拆分一下。

    cat Dockerfile
    FROM registry.xxx.com/base/oracle-jdk:8u201 as builder
    WORKDIR /app
    COPY --chown=1001:1001 ./xxx-1.0.0.jar /app/lib/xxx-1.0.0.jar
    RUN java -Djarmode=layertools -jar /app/lib/xxx-1.0.0.jar extract

    一階段實際就是將jar解壓按照spring boot layers.idx檔中的分層方式將依賴和業務代分碼層。

    "dependencies":
    - BOOT-INF/lib/library1.jar
    - BOOT-INF/lib/library2.jar
    "spring-boot-loader":
    - org/springframework/boot/loader/JarLauncher. class
    - org/springframework/boot/loader/jar/JarEntry. class
    "snapshot-dependencies":
    - BOOT-INF/lib/library3-SNAPSHOT.jar
    "application":
    - META-INF/MANIFEST.MF
    - BOOT-INF/ classes/a/b/C. class

    進入到容器中可以看到被解壓為4個目錄:

    [root@b762a63589c1 app]# ls
    application dependencies lib logs snapshot-dependencies spring-boot-loader

    儲存占用如下:其中不出意外dependencies也就是依賴庫占用是最大的 業務程式碼只有可憐的304K🤔。

    [root@29d87aaf64b9 app]# du -lh --max-depth=1
    304K ./application
    65M ./dependencies
    0 ./logs
    20K ./snapshot-dependencies
    420K ./spring-boot-loader
    65M ./lib
    130M .

    基於分層構建構建制品映像

    在官方的demo上修改得到完整的Dockerfile。其中最佳化了entrypoint指令碼,采用分層構建後啟動套用不能采用傳統的 java $JAVA_OPTS -jar xxx.jar 這方式啟動套用。要使用 java org.springframework.boot.loader.JarLauncher 啟動套用因此jvm參數需要透過指令碼接受容器的環境變量來傳入。

    FROM registry.xxx.com/base/oracle-jdk:8u201 as builder
    WORKDIR /app
    COPY --chown=1001:1001 ./xxx-1.0.0.jar /app/lib/xxx-1.0.0.jar
    RUN java -Djarmode=layertools -jar /app/lib/xxx-1.0.0.jar extract
    FROM registry.xxx.com/base/oracle-jdk:8u201
    ENV TZ=Asia/Shanghai
    ENV LC_ALL en_US.utf8
    RUN mkdir -pv /app/logs && ln -s /app/logs /app/log && chown 1001:1001 -R /app
    WORKDIR /app
    COPY --chown=1001:1001 --from=builder /app/dependencies/ ./
    COPY --chown=1001:1001 --from=builder /app/spring-boot-loader/ ./
    COPY --chown=1001:1001 --from=builder /app/snapshot-dependencies/ ./
    COPY --chown=1001:1001 --from=builder /app/application/ ./
    CMD ["exec"]
    ENTRYPOINT ["bash","/app/entrypoint.sh"]

    entrypoint.sh指令碼使用exec將java作為容器1號行程,避免無法接受linux 訊號導致java行程無法正常結束。

    #!/bin/bash
    if [ -f /opt/cm/before.sh ]; then
    source /opt/cm/before.sh;
    fi
    exec java $JAVA_OPTS org.springframework.boot.loader.JarLauncher

    構建映像

    [root@localhost new]# docker build -t registry.xxx.com/layer/build:new .
    Sending build context to Docker daemon 67.45MB
    Step 1/14 : FROM registry.xxx.com/base/oracle-jdk:8u201 as builder
     ---> 1869b73a8999
    Step 2/14 : WORKDIR /app
     ---> Using cache
     ---> 6973e49f31fa
    Step 3/14 : COPY --chown=1001:1001 ./xxx-1.0.0.jar /app/lib/xxx-1.0.0.jar
     ---> Using cache
     ---> c5a17d5969ea
    Step 4/14 : RUN java -Djarmode=layertools -jar /app/lib/xxx-1.0.0.jar extract
     ---> Using cache
     ---> 911074c3ef21
    Step 5/14 : FROM registry.xxx.com/base/oracle-jdk:8u201
     ---> 1869b73a8999
    Step 6/14 : ENV TZ=Asia/Shanghai
     ---> Using cache
     ---> 9304102c278e
    Step 7/14 : ENV LC_ALL en_US.utf8
     ---> Using cache
     ---> 63fb6c823893
    Step 8/14 : RUN mkdir -pv /app/logs && ln -s /app/logs /app/log && chown 1001:1001 -R /app
     ---> Running in 9cac9cb86507
    mkdir: created directory ‘/app’
    mkdir: created directory ‘/app/logs’
    Removing intermediate container 9cac9cb86507
     ---> 858e77315e6b
    Step 9/14 : WORKDIR /app
     ---> Running in 81c7173692fc
    Removing intermediate container 81c7173692fc
     ---> 5c19ddda3114
    Step 10/14 : COPY --chown=1001:1001 --from=builder /app/dependencies/ ./
     ---> 078e3f4a199c
    Step 11/14 : COPY --chown=1001:1001 --from=builder /app/spring-boot-loader/ ./
     ---> 1542209ccc8b
    Step 12/14 : COPY --chown=1001:1001 --from=builder /app/snapshot-dependencies/ ./
     ---> 75a9584e0952
    Step 13/14 : COPY --chown=1001:1001 --from=builder /app/application/ ./
     ---> 555270edceea
    Step 14/14 : ENTRYPOINT ["java""org.springframework.boot.loader.JarLauncher"]
     ---> Running in fe99278683a2
    Removing intermediate container fe99278683a2
     ---> 2cce0deab7e2
    Successfully built 2cce0deab7e2
    Successfully tagged registry.xxx.com/layer/build:new

    檢視分層詳情,可以看到jar被拆分成4層分別由copy指令建立。

    [root@localhost ~]# docker image history registry.xxx.com/layer/build:new
    IMAGE CREATED CREATED BY SIZE COMMENT
    2cce0deab7e2 28 hours ago /bin/sh -c #(nop) ENTRYPOINT ["java" "org.s… 0B
    555270edceea 28 hours ago /bin/sh -c #(nop) COPY dir:2f5975d11563ae449… 248kB
    75a9584e0952 28 hours ago /bin/sh -c #(nop) COPY dir:a3bb0870001a0ef25… 18.5kB
    1542209ccc8b 28 hours ago /bin/sh -c #(nop) COPY dir:16d00ea1f180914e2… 252kB
    078e3f4a199c 28 hours ago /bin/sh -c #(nop) COPY dir:1d3190ed598b634a5… 67.1MB
    5c19ddda3114 28 hours ago /bin/sh -c #(nop) WORKDIR /app 0B
    858e77315e6b 28 hours ago /bin/sh -c mkdir -pv /app/logs && ln -s /app… 9B
    63fb6c823893 30 hours ago /bin/sh -c #(nop) ENV LC_ALL=en_US.utf8 0B
    9304102c278e 30 hours ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai 0B
    1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=currycan… 0B
    <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB

    對比發現jar包被拆分了。

    [root@localhost new]# diff old.info new.info
    1,16c1,18
    < IMAGE CREATED CREATED BY SIZE COMMENT
    < b9fc3a8a97f2 About an hour ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/sh" "-c… 0B
    < c823448f9582 About an hour ago /bin/sh -c #(nop) COPY --chown=1001:1001file… 128B
    < 9a997f3962bf About an hour ago /bin/sh -c #(nop) COPY --chown=1001:1001file… 67.4MB
    < f272c4e64f87 About an hour ago /bin/sh -c #(nop) WORKDIR /app 0B
    < 1a526d3df7c2 About an hour ago /bin/sh -c mkdir -pv /app/{bin,lib,logs} && … 9B
    < 63fb6c823893 2 hours ago /bin/sh -c #(nop) ENV LC_ALL=en_US.utf8 0B
    < 9304102c278e 2 hours ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai 0B
    < 1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    < <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    < <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    < <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    < <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=currycan… 0B
    < <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    < <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    < <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
    ---
    > IMAGE CREATED CREATED BY SIZE COMMENT
    > 2cce0deab7e2 8 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["java" "org.s… 0B
    > 555270edceea 8 minutes ago /bin/sh -c #(nop) COPY dir:2f5975d11563ae449… 248kB
    > 75a9584e0952 8 minutes ago /bin/sh -c #(nop) COPY dir:a3bb0870001a0ef25… 18.5kB
    > 1542209ccc8b 8 minutes ago /bin/sh -c #(nop) COPY dir:16d00ea1f180914e2… 252kB
    > 078e3f4a199c 8 minutes ago /bin/sh -c #(nop) COPY dir:1d3190ed598b634a5… 67.1MB
    > 5c19ddda3114 8 minutes ago /bin/sh -c #(nop) WORKDIR /app 0B
    > 858e77315e6b 8 minutes ago /bin/sh -c mkdir -pv /app/logs && ln -s /app… 9B
    > 63fb6c823893 2 hours ago /bin/sh -c #(nop) ENV LC_ALL=en_US.utf8 0B
    > 9304102c278e 2 hours ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai 0B
    > 1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    > <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    > <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    > <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    > <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=currycan… 0B
    > <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    > <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    > <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB

    推播到倉庫

    [root@localhost new]# docker push registry.xxx.com/layer/build:new
    The push refers to repository [registry.xxx.com/layer/build]
    e5409ea62302: Pushed
    7167dfa406ea: Pushed
    cf7e94a2dc11: Pushed
    8e261e1bae2c: Pushed
    770c31a84800: Pushed
    c33745b35097: Layer already exists
    2105884a1756: Layer already exists
    174f56854903: Layer already exists
    new: digest: sha256:faef10681394c01d1c2973ed10c7c33e7289eec25f22c90d085f6eef96ce3bff size: 2001

    驗證模擬開發推播程式碼更新映像

    開發摸了一天🐟終於開發出新版本

    [root@localhost new]# md5sum bc-1.0.1.jar bc-1.0.0.jar
    3792d3178a4e07708052b7effa9b12f8 xxx-1.0.1.jar
    8ee570b94977668631355cb49c5d7cf1 xxx-1.0.0.jar

    構建新版本,構建過程階段二中使用了cache 僅更新了 application層

    [root@localhost new]# docker image build -t registry.xxx.com/layer/build:new-1.0.1 .
    Sending build context to Docker daemon 134.9MB
    Step 1/16 : FROM registry.xxx.com/base/oracle-jdk:8u201 as builder
     ---> 1869b73a8999
    Step 2/16 : WORKDIR /app
     ---> Using cache
     ---> 6973e49f31fa
    Step 3/16 : COPY --chown=1001:1001 ./xxx-1.0.1.jar /app/lib/xxx-1.0.1.jar
     ---> b219210fd8ab
    Step 4/16 : RUN java -Djarmode=layertools -jar /app/lib/xxx-1.0.1.jar extract
     ---> Running in d099c10b653c
    Removing intermediate container d099c10b653c
     ---> 66a8593b0291
    Step 5/16 : FROM registry.xxx.com/base/oracle-jdk:8u201
     ---> 1869b73a8999
    Step 6/16 : ENV TZ=Asia/Shanghai
     ---> Using cache
     ---> 9304102c278e
    Step 7/16 : ENV LC_ALL en_US.utf8
     ---> Using cache
     ---> 63fb6c823893
    Step 8/16 : RUN mkdir -pv /app/logs && ln -s /app/logs /app/log && chown 1001:1001 -R /app
     ---> Using cache
     ---> 858e77315e6b
    Step 9/16 : WORKDIR /app
     ---> Using cache
     ---> 5c19ddda3114
     Step 10/16 : COPY --chown=1001:1001 ./entrypoint.sh /app/entrypoint.sh
     ---> Using cache
     ---> c47b74bb8cf9
    Step 11/16 : COPY --chown=1001:1001 --from=builder /app/dependencies/ ./
     ---> Using cache
     ---> 078e3f4a199c
    Step 12/16 : COPY --chown=1001:1001 --from=builder /app/spring-boot-loader/ ./
     ---> Using cache
     ---> 1542209ccc8b
    Step 13/16 : COPY --chown=1001:1001 --from=builder /app/snapshot-dependencies/ ./
     ---> Using cache
     ---> 75a9584e0952
    Step 14/16 : COPY --chown=1001:1001 --from=builder /app/application/ ./
     ---> 0f138b106caf
    Step 15/16 : CMD ["exec"]
     ---> Using cache
     ---> a1170ae7a20e
    Step 16/16 : ENTRYPOINT ["bash","/app/entrypoint.sh"]
     ---> Using cache
     ---> 5a6bb7613fc8
    Successfully built 5a6bb7613fc8
    Successfully tagged registry.xxx.com/layer/build:new-1.0.1

    推播到倉庫可以看到僅推播了application層 只有300K

    docker push registry.xxx.com/layer/build:new-1.0.1
    The push refers to repository [registry.xxx.com/layer/build]
    c33a5c6456bd: Pushed
    7167dfa406ea: Layer already exists
    cf7e94a2dc11: Layer already exists
    8e261e1bae2c: Layer already exists
    770c31a84800: Layer already exists
    c33745b35097: Layer already exists
    2105884a1756: Layer already exists
    174f56854903: Layer already exists
    new-1.0.1: digest: sha256:53647bb45f0f0bb5bb8d56f14cd40deb22f0d5e4fce1200fed1c225a516dddb5 size: 2001

    驗證映像

    [root@localhost new]# docker run -it --rm -u 1001:1001 -e JAVA_OPTS="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:+PrintCommandLineFlags" registry.xxx.com/layer/build:new-1.0.1

    可以看到打印出了堆疊資訊(容器預設是不開啟的)套用成功啟動😋

    [root@localhost ~]# docker run -it --rm -u 1001:1001 -e JAVA_OPTS="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:+PrintCommandLineFlags" registry.xxx.com/layer/build:new-1.0.1
    -XX:InitialHeapSize=130478208 -XX:MaxHeapSize=2087651328 -XX:+PrintCommandLineFlags -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+UseCompressed classPointers -XX:+UseCompressedOops -XX:+UseParallelGC
    {Heap before GC invocations=1 (full 0):
     PSYoungGen total 37888K, used 32768K [0x00000000d6800000, 0x00000000d9200000, 0x0000000100000000)
    eden space 32768K, 100% used [0x00000000d6800000,0x00000000d8800000,0x00000000d8800000)
    from space 5120K, 0% used [0x00000000d8d00000,0x00000000d8d00000,0x00000000d9200000)
    to space 5120K, 0% used [0x00000000d8800000,0x00000000d8800000,0x00000000d8d00000)
     ParOldGen total 86016K, used 0K [0x0000000083800000, 0x0000000088c00000, 0x00000000d6800000)
    eden space 32768K, 0% used [0x00000000d6800000,0x00000000d6800000,0x00000000d8800000)
    from space 5120K, 72% used [0x00000000d8800000,0x00000000d8ba1748,0x00000000d8d00000)
    to space 5120K, 0% used [0x00000000dad00000,0x00000000dad00000,0x00000000db200000)
     ParOldGen total 86016K, used 8K [0x0000000083800000, 0x0000000088c00000, 0x00000000d6800000)
    object space 86016K, 0% used [0x0000000083800000,0x0000000083802000,0x0000000088c00000)
     Metaspace used 6799K, capacity 7074K, committed 7168K, reserved 1056768K
    class space used 820K, capacity 935K, committed 1024K, reserved 1048576K
    }
    ...
    2023-08-17 20:50:42.253 INFO 1 --- [ main] org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver.<init>(EndpointLinksResolver.java:58) - [] : Exposing 1 endpoint(s) beneath base path '/actuator'
    2023-08-17 20:50:42.315 INFO 1 --- [ main] org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173) - [] : Starting ProtocolHandler ["http-nio-8082"]
    2023-08-17 20:50:42.349 INFO 1 --- [ main] org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:220) - [] : Tomcat started on port(s): 8082 (http) with context path '/xxx'
    2023-08-17 20:50:42.376 INFO 1 --- [ main] org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:61) - [] : Started Application in 11.242 seconds (JVM running for 12.553)

    對比兩個版本可以發現只有application成hash值不一樣 程式碼量增加了 253kB-248kB=5KB

    [root@localhost new]# docker image history registry.xxx.com/layer/build:new
    IMAGE CREATED CREATED BY SIZE COMMENT
    2cce0deab7e2 About an hour ago /bin/sh -c #(nop) ENTRYPOINT ["java" "org.s… 0B
    555270edceea About an hour ago /bin/sh -c #(nop) COPY dir:2f5975d11563ae449… 248kB
    75a9584e0952 About an hour ago /bin/sh -c #(nop) COPY dir:a3bb0870001a0ef25… 18.5kB
    1542209ccc8b About an hour ago /bin/sh -c #(nop) COPY dir:16d00ea1f180914e2… 252kB
    078e3f4a199c About an hour ago /bin/sh -c #(nop) COPY dir:1d3190ed598b634a5… 67.1MB
    5c19ddda3114 About an hour ago /bin/sh -c #(nop) WORKDIR /app 0B
    858e77315e6b About an hour ago /bin/sh -c mkdir -pv /app/logs && ln -s /app… 9B
    63fb6c823893 3 hours ago /bin/sh -c #(nop) ENV LC_ALL=en_US.utf8 0B
    9304102c278e 3 hours ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai 0B
    1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=currycan… 0B
    <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
    [root@localhost new]# docker image history registry.xxx.com/layer/build:new-1.0.1
    IMAGE CREATED CREATED BY SIZE COMMENT
    76aa6233354a 40 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["java" "org.s… 0B
    0f138b106caf 40 minutes ago /bin/sh -c #(nop) COPY dir:4d21cc5ce0d92794b… 253kB
    75a9584e0952 About an hour ago /bin/sh -c #(nop) COPY dir:a3bb0870001a0ef25… 18.5kB
    1542209ccc8b About an hour ago /bin/sh -c #(nop) COPY dir:16d00ea1f180914e2… 252kB
    078e3f4a199c About an hour ago /bin/sh -c #(nop) COPY dir:1d3190ed598b634a5… 67.1MB
    5c19ddda3114 About an hour ago /bin/sh -c #(nop) WORKDIR /app 0B
    858e77315e6b About an hour ago /bin/sh -c mkdir -pv /app/logs && ln -s /app… 9B
    63fb6c823893 3 hours ago /bin/sh -c #(nop) ENV LC_ALL=en_US.utf8 0B
    9304102c278e 3 hours ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai 0B
    1869b73a8999 2 years ago /bin/sh -c set -ex; cd /tmp; curl -fsSLO… 302MB
    <missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION=8 JAVA_U… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ENV TZ=Asia/Shanghai LANG… 0B
    <missing> 2 years ago /bin/sh -c set -ex; yum update -y; yum i… 116MB
    <missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=currycan… 0B
    <missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
    <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
    <missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB

    上述對開發沒有惡意,大家都是辛苦勤勞上進的打工人。 哈哈哈又是摸🐟的一天 ---Author Logn

    參照:[spring boot官方文件](Container Images (spring.io))

    👉 歡迎 ,你將獲得: 專屬的計畫實戰 / Java 學習路線 / 一對一提問 / 學習打卡 / 每月贈書

    新計畫: 仿小紅書 (微服務架構)正在更新中... , 全棧前後端分離部落格計畫 2.0 版本完結啦, 演示連結 http://116.62.199.48/ 。全程手摸手,後端 + 前端全棧開發,從 0 到 1 講解每個功能點開發步驟,1v1 答疑,直到計畫上線。 目前已更新了261小節,累計41w+字,講解圖:1806張,還在持續爆肝中.. 後續還會上新更多計畫,目標是將Java領域典型的計畫都整一波,如秒殺系統, 線上商城, IM即時通訊,Spring Cloud Alibaba 等等,


    1. 

    2. 

    3. 

    4. 

    最近面試BAT,整理一份面試資料Java面試BATJ通關手冊,覆蓋了Java核心技術、JVM、Java並行、SSM、微服務、資料庫、數據結構等等。

    獲取方式:點「在看」,關註公眾號並回復 Java 領取,更多內容陸續奉上。

    PS:因公眾號平台更改了推播規則,如果不想錯過內容,記得讀完點一下在看,加個星標,這樣每次新文章推播才會第一時間出現在你的訂閱列表裏。

    「在看」支持小哈呀,謝謝啦