亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
1. Use a Smaller Base Image
2. Build and Run in Separate Stages (Multi-Stage Builds)
3. Optimize the JAR Itself
4. Enable Class Data Sharing (CDS)
5. Tune JVM Settings for Containers
6. Consider Native Images (GraalVM) for Extreme Optimization
Summary of Gains
首頁 Java java教程 優(yōu)化尺寸較小和更快啟動(dòng)的Java Docker圖像

優(yōu)化尺寸較小和更快啟動(dòng)的Java Docker圖像

Jul 29, 2025 am 12:27 AM
java docker

使用更小的基礎(chǔ)鏡像如eclipse-temurin:17-jre-alpine或-slim以減少體積;2. 采用多階段構(gòu)建分離編譯與運(yùn)行環(huán)境,避免將構(gòu)建工具和源碼打入最終鏡像;3. 優(yōu)化JAR包本身,移除無用依賴、啟用壓縮并考慮Spring Boot分層JAR;4. 啟用類數(shù)據(jù)共享(CDS)以降低啟動(dòng)時(shí)間和內(nèi)存占用;5. 調(diào)整JVM容器化參數(shù)如-XX: UseContainerSupport和-XX:MaxRAMPercentage以適配容器資源限制;6. 對(duì)啟動(dòng)速度要求極高的場(chǎng)景可選用GraalVM原生鏡像,顯著提升性能但增加構(gòu)建復(fù)雜度;綜合使用前五項(xiàng)可在低復(fù)雜度下獲得良好優(yōu)化效果,而原生鏡像適合對(duì)冷啟動(dòng)敏感的云原生應(yīng)用。

Optimizing Java Docker Images for Smaller Size and Faster Startup

Optimizing Java Docker images isn’t just about shrinking file sizes—it’s about faster deployments, reduced attack surface, and quicker startup times, especially in cloud and serverless environments. Here’s how to make your Java Docker images leaner and more efficient.

Optimizing Java Docker Images for Smaller Size and Faster Startup

1. Use a Smaller Base Image

The base image you choose has a massive impact on size. Traditional openjdk:17-jdk or openjdk:17 images are based on full Linux distributions like Debian and can be over 500MB.

Better options:

Optimizing Java Docker Images for Smaller Size and Faster Startup
  • eclipse-temurin:17-jre-alpine – Alpine Linux is tiny (~5MB base), and JRE-only reduces footprint.
  • eclipse-temurin:17-jre-slim – Debian-based but stripped down. More compatible than Alpine, still much smaller than full JDK.
# Instead of:
# FROM openjdk:17
# Use:
FROM eclipse-temurin:17-jre-alpine

?? Note: Alpine uses musl instead of glibc, which can cause compatibility issues with some native libraries. If you hit issues, go with -slim instead.


2. Build and Run in Separate Stages (Multi-Stage Builds)

Avoid bundling build tools, source code, and dependencies into your final image.

Optimizing Java Docker Images for Smaller Size and Faster Startup
# Multi-stage: build with full JDK, run with JRE
FROM eclipse-temurin:17-jdk-alpine AS builder
WORKDIR /app
COPY . .
RUN ./gradlew build -x test

FROM eclipse-temurin:17-jre-alpine
WORKDIR /app
COPY --from=builder /app/build/libs/app.jar app.jar
CMD ["java", "-jar", "app.jar"]

This keeps the runtime image minimal—no Gradle, source files, or dev tools.


3. Optimize the JAR Itself

A fat JAR (uber-jar) with all dependencies included is convenient but can be bloated.

Tips:

  • Remove unused dependencies – Use tools like dependency-check or manual review.
  • Use Spring Boot’s thin JAR (if applicable) – Offload dependencies to a shared layer.
  • Enable JAR compression – Most build tools do this by default, but verify.

Pro tip: If using Spring Boot, consider Spring Boot 3.2 with container image support that layers JAR content for better Docker layer caching.


4. Enable Class Data Sharing (CDS)

Class Data Sharing allows the JVM to preload and memory-map core classes, reducing startup time and memory usage.

Generate a CDS archive during image build:

FROM eclipse-temurin:17-jre-alpine
COPY app.jar /app.jar

# Generate CDS archive
RUN java -Xshare:dump -XX:ArchiveClassesAtExit=/app.jsa -jar /app.jar
# Or if you know the main class:
# RUN java -cp app.jar -Xshare:dump -XX:ArchiveClassesAtExit=/app.jsa com.example.Main

# Use CDS at runtime
ENTRYPOINT ["java", "-Xshare:auto", "-XX:SharedArchiveFile=/app.jsa", "-jar", "app.jar"]

This can cut startup time by 10–30%, especially for apps with many dependencies.


5. Tune JVM Settings for Containers

By default, the JVM may not respect container memory limits.

Add these flags:

ENTRYPOINT ["java", \
  "-XX: UseContainerSupport", \
  "-XX:MaxRAMPercentage=75.0", \
  "-XshowSettings:vm", \
  "-jar", "app.jar"]
  • UseContainerSupport lets JVM detect container limits.
  • MaxRAMPercentage avoids over-allocating heap.

This prevents OOM kills and improves startup predictability.


6. Consider Native Images (GraalVM) for Extreme Optimization

For the fastest startup and smallest footprint, compile your Java app to a native binary using GraalVM Native Image.

Pros:

  • Startup in milliseconds.
  • Smaller memory footprint.
  • Smaller image (no JVM needed).

Cons:

  • Longer build time.
  • Limited reflection/dynamic classloading (requires configuration).
  • Larger build image.

Example:

# Build stage with GraalVM
FROM ghcr.io/graalvm/graalvm-jdk:17 AS builder
RUN gu install native-image
COPY . .
RUN native-image -jar app.jar

# Final stage
FROM alpine:latest
COPY --from=builder app /app
ENTRYPOINT ["./app"]

Best for microservices, serverless, or CLI tools where fast startup is critical.


Summary of Gains

Optimization Size Reduction Startup Improvement
Alpine/JRE base 30–50% Minor
Multi-stage build 20–40% None
CDS Minimal 10–30%
Native image 50–80% 80–95% faster

Pick the right combo based on your needs. For most apps, slim base multi-stage CDS gives excellent results without complexity. For cloud-native services, GraalVM native is worth the investment.

Basically, don’t ship a data center’s worth of JDK just to run one JAR.

以上是優(yōu)化尺寸較小和更快啟動(dòng)的Java Docker圖像的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

Java項(xiàng)目管理Maven的開發(fā)人員指南 Java項(xiàng)目管理Maven的開發(fā)人員指南 Jul 30, 2025 am 02:41 AM

Maven是Java項(xiàng)目管理和構(gòu)建的標(biāo)準(zhǔn)工具,答案在於它通過pom.xml實(shí)現(xiàn)項(xiàng)目結(jié)構(gòu)標(biāo)準(zhǔn)化、依賴管理、構(gòu)建生命週期自動(dòng)化和插件擴(kuò)展;1.使用pom.xml定義groupId、artifactId、version和dependencies;2.掌握核心命令如mvnclean、compile、test、package、install和deploy;3.利用dependencyManagement和exclusions管理依賴版本與衝突;4.通過多模塊項(xiàng)目結(jié)構(gòu)組織大型應(yīng)用並由父POM統(tǒng)一管理;5.配

用雅加達(dá)EE在Java建立靜止的API 用雅加達(dá)EE在Java建立靜止的API Jul 30, 2025 am 03:05 AM

SetupaMaven/GradleprojectwithJAX-RSdependencieslikeJersey;2.CreateaRESTresourceusingannotationssuchas@Pathand@GET;3.ConfiguretheapplicationviaApplicationsubclassorweb.xml;4.AddJacksonforJSONbindingbyincludingjersey-media-json-jackson;5.DeploytoaJakar

Python物業(yè)裝飾示例 Python物業(yè)裝飾示例 Jul 30, 2025 am 02:17 AM

@property裝飾器用於將方法轉(zhuǎn)為屬性,實(shí)現(xiàn)屬性的讀取、設(shè)置和刪除控制。 1.基本用法:通過@property定義只讀屬性,如area根據(jù)radius計(jì)算並直接訪問;2.進(jìn)階用法:使用@name.setter和@name.deleter實(shí)現(xiàn)屬性的賦值驗(yàn)證與刪除操作;3.實(shí)際應(yīng)用:在setter中進(jìn)行數(shù)據(jù)驗(yàn)證,如BankAccount確保餘額非負(fù);4.命名規(guī)範(fàn):內(nèi)部變量用_前綴,property方法名與屬性一致,通過property統(tǒng)一訪問控制,提升代碼安全性和可維護(hù)性。

CSS暗模式切換示例 CSS暗模式切換示例 Jul 30, 2025 am 05:28 AM

首先通過JavaScript獲取用戶系統(tǒng)偏好和本地存儲(chǔ)的主題設(shè)置,初始化頁面主題;1.HTML結(jié)構(gòu)包含一個(gè)按鈕用於觸發(fā)主題切換;2.CSS使用:root定義亮色主題變量,.dark-mode類定義暗色主題變量,並通過var()應(yīng)用這些變量;3.JavaScript檢測(cè)prefers-color-scheme並讀取localStorage決定初始主題;4.點(diǎn)擊按鈕時(shí)切換html元素上的dark-mode類,並將當(dāng)前狀態(tài)保存至localStorage;5.所有顏色變化均帶有0.3秒過渡動(dòng)畫,提升用戶

如何將Java MistageDigest用於哈希(MD5,SHA-256)? 如何將Java MistageDigest用於哈希(MD5,SHA-256)? Jul 30, 2025 am 02:58 AM

要使用Java生成哈希值,可通過MessageDigest類實(shí)現(xiàn)。 1.獲取指定算法的實(shí)例,如MD5或SHA-256;2.調(diào)用.update()方法傳入待加密數(shù)據(jù);3.調(diào)用.digest()方法獲取哈希字節(jié)數(shù)組;4.將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)製字符串以便讀??;對(duì)於大文件等輸入,應(yīng)分塊讀取並多次調(diào)用.update();推薦使用SHA-256而非MD5或SHA-1以確保安全性。

CSS下拉菜單示例 CSS下拉菜單示例 Jul 30, 2025 am 05:36 AM

是的,一個(gè)常見的CSS下拉菜單可以通過純HTML和CSS實(shí)現(xiàn),無需JavaScript。 1.使用嵌套的ul和li構(gòu)建菜單結(jié)構(gòu);2.通過:hover偽類控制下拉內(nèi)容的顯示與隱藏;3.父級(jí)li設(shè)置position:relative,子菜單使用position:absolute進(jìn)行定位;4.子菜單默認(rèn)display:none,懸停時(shí)變?yōu)閐isplay:block;5.可通過嵌套實(shí)現(xiàn)多級(jí)下拉,結(jié)合transition添加淡入動(dòng)畫,配合媒體查詢適配移動(dòng)端,整個(gè)方案簡(jiǎn)潔且無需JavaScript支持,適合大

如何將Docker與VS代碼一起使用? 如何將Docker與VS代碼一起使用? Jul 30, 2025 am 02:29 AM

InstallDockerDesktop,VSCode,andtheofficialDockerextensionfromMicrosoft.2.CreateaDockerfileinyourprojectroot,suchasusingnode:18-alpineforNode.jsappswithproperCOPY,RUN,andCMDinstructions.3.UsetheDockerextensionpaneltobuildtheimage,thenrunitasacontainer

Python Parse Date String示例 Python Parse Date String示例 Jul 30, 2025 am 03:32 AM

使用datetime.strptime()可將日期字符串轉(zhuǎn)換為datetime對(duì)象,1.基本用法:通過"%Y-%m-%d"解析"2023-10-05"為datetime對(duì)象;2.支持多種格式如"%m/%d/%Y"解析美式日期、"%d/%m/%Y"解析英式日期、"%b%d,%Y%I:%M%p"解析帶AM/PM的時(shí)間;3.可用dateutil.parser.parse()自動(dòng)推斷未知格式;4.使用.d

See all articles