您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)ARM架構(gòu)下的Docker環(huán)境沒(méi)有OpenJDK=8版本鏡像該如何解決,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
對(duì)現(xiàn)有的Java應(yīng)用,之前一直運(yùn)行在x86處理器環(huán)境下,編譯和運(yùn)行都是JDK8,如今在樹(shù)莓派的Docker環(huán)境運(yùn)行(或者其他ARM架構(gòu)電腦,例如華為泰山服務(wù)器),需要JDK8鏡像作為基礎(chǔ)鏡像。
在《ARM64架構(gòu)下,OpenJDK的官方Docker鏡像為何沒(méi)有8版本?》一文中,已經(jīng)確定了OpenJDK官方并未提供8版本的Docker鏡像,因此,原有的Java應(yīng)用,如果是基于JDK8編譯和運(yùn)行的,現(xiàn)在從X86架構(gòu)轉(zhuǎn)戰(zhàn)到ARM架構(gòu)的Docker環(huán)境下,就會(huì)面臨沒(méi)有JDK基礎(chǔ)鏡像的問(wèn)題;
《ARM64架構(gòu)下,OpenJDK的官方Docker鏡像為何沒(méi)有8版本?》一文曾經(jīng)提到應(yīng)對(duì)之道:
自己編譯一個(gè)8版本的OpenJDK安裝包,以此來(lái)做Docker鏡像;
Oracle提供了ARM版本的JDKD安裝包,以此包來(lái)做Docker鏡像;
用OpenJDK的11版本,但是11和8的差異要自行處理;
對(duì)于第一種方式,自己編譯8版本的OpenJDK,難度太大(對(duì)我自己而言),因?yàn)榫幾gOpenJDK需要低版本的OpenJDK作為編譯工具,也就是說(shuō)我要找到ARM版本的OpenJDK7,才能編譯ARM版本的OpenJDK8,因此我覺(jué)得這樣做的難度太大…
今天要討論的是第二種和第三種,
硬件:樹(shù)莓派4B
操作系統(tǒng):openfans的64為Debian
Docker:19.03.1
docker-compose:1.24.1
本文要解決的問(wèn)題是ARM架構(gòu)的電腦上,如何在Docker環(huán)境運(yùn)行Java應(yīng)用,因此需要有個(gè)Java應(yīng)用來(lái)驗(yàn)證,這里找了個(gè)最普通的SpringBoot應(yīng)用,提供一個(gè)hello world的http接口,通過(guò)jib插件構(gòu)建成Docker鏡像,整個(gè)應(yīng)用的源碼可以從GitHub上下載,地址和鏈接信息如下表所示:
名稱(chēng) | 鏈接 | 備注 |
---|---|---|
項(xiàng)目主頁(yè) | https://github.com/zq2599/blog_demos | 該項(xiàng)目在GitHub上的主頁(yè) |
git倉(cāng)庫(kù)地址(https) | https://github.com/zq2599/blog_demos.git | 該項(xiàng)目源碼的倉(cāng)庫(kù)地址,https協(xié)議 |
git倉(cāng)庫(kù)地址(ssh) | git@github.com:zq2599/blog_demos.git | 該項(xiàng)目源碼的倉(cāng)庫(kù)地址,ssh協(xié)議 |
</br>
這個(gè)git項(xiàng)目中有多個(gè)文件夾,本章的源碼在hellojib文件夾下,如下圖紅框所示:
接下來(lái)的操作步驟,如下圖所示:
要想在ARM機(jī)器上編譯構(gòu)建hellojib工程,就要把JDK和Maven裝好,先裝JDK;
去Oracle網(wǎng)站下載ARM版本的JDK8,地址是:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ,如下圖紅框所示:
上述JDK文件解壓后是個(gè)名為<font color="blue">jdk1.8.0_221</font>的文件夾,將此文件夾放在ARM電腦的<font color="blue">/usr/lib/jvm</font>目錄下;
打開(kāi)文件<font color="blue">~/.bashrc</font>,增加以下內(nèi)容:
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_221 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH
執(zhí)行<font color="blue">source ~/.bashrc</font>,使得配置立即生效;
執(zhí)行命令<font color="blue">java -version</font>試試JDK是否已經(jīng)可用:
root@raspbian:~# java -version java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
去Maven官網(wǎng)下載安裝包,我這里下載的是3.6.2版本,地址:https://www-eu.apache.org/dist/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
安裝包解壓之后是個(gè)名為<font color="blue">apache-maven-3.6.2</font>的文件夾,將此文件夾放在ARM電腦的<font color="blue">/usr/local</font>目錄下;
打開(kāi)文件<font color="blue">~/.bashrc</font>,增加以下內(nèi)容:
export MAVEN_HOME=/usr/local/apache-maven-3.6.2 export PATH=$MAVEN_HOME/bin:$PATH
執(zhí)行<font color="blue">source ~/.bashrc</font>,使得配置立即生效;
執(zhí)行命令<font color="blue">mvn -version</font>試試maven是否已經(jīng)可用:
root@raspbian:~# mvn -version Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00) Maven home: /usr/local/apache-maven-3.6.2 Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk1.8.0_221/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.19.69-openfans+20190906-v8", arch: "aarch74", family: "unix"
折騰了這么久,總算是完成了準(zhǔn)備工作,接下來(lái)開(kāi)始做作鏡像了;
要把咱們自己的Java應(yīng)用做成Docker鏡像,需要有個(gè)JDK8鏡像作為基礎(chǔ)鏡像,接下來(lái)我們來(lái)做這個(gè)基礎(chǔ)鏡像;
在ARM電腦上新建一個(gè)文件夾,里面新建名為<font color="blue">Dockerfile</font>的文件,內(nèi)容如下:
#Docker image of JDK8 in ARM64 # VERSION 8 # Author: bolingcavalry #基礎(chǔ)鏡像使用的是OpenJDK官方鏡像公用的 FROM buildpack-deps:stretch-scm #作者 MAINTAINER BolingCavalry <zq2599@gmail.com> # Default to UTF-8 file.encoding ENV LANG C.UTF-8 ENV JAVA_HOME /usr/local/jdk8 ENV PATH $JAVA_HOME/bin:$PATH ENV JDK_FILE jdk-8u221-linux-arm64-vfp-hflt.tar.gz COPY $JDK_FILE /usr/local/ RUN mkdir -p "$JAVA_HOME"; \ tar --extract \ --file /usr/local/$JDK_FILE \ --directory "$JAVA_HOME" \ --strip-components 1 \ --no-same-owner; \ rm /usr/local/$JDK_FILE
將之前下載的<font color="blue">jdk-8u221-linux-arm64-vfp-hflt.tar.gz</font>文件復(fù)制到Dockerfile文件所在目錄;
在Dockerfile文件所在目錄執(zhí)行命令<font color="blue">docker build -t bolingcavalry/arm64jdk:8 .</font>(注意命令的末尾有個(gè)小數(shù)點(diǎn),不要漏了)
執(zhí)行成功后控制臺(tái)輸出以下信息:
root@raspbian:~/test# docker build -t bolingcavalry/arm64jdk:8 . Sending build context to Docker daemon 73.2MB Step 1/8 : FROM buildpack-deps:stretch-scm ---> 1838b930d30a Step 2/8 : MAINTAINER BolingCavalry <zq2599@gmail.com> ---> Using cache ---> ce7488aef612 Step 3/8 : ENV LANG C.UTF-8 ---> Using cache ---> 0bdb9ce285a9 Step 4/8 : ENV JAVA_HOME /usr/local/jdk8 ---> Using cache ---> 39a412f0d874 Step 5/8 : ENV PATH $JAVA_HOME/bin:$PATH ---> Using cache ---> 1a3077d36d4e Step 6/8 : ENV JDK_FILE jdk-8u221-linux-arm64-vfp-hflt.tar.gz ---> Using cache ---> c39af69e10f7 Step 7/8 : COPY $JDK_FILE /usr/local/ ---> Using cache ---> 5fc704c5b9ff Step 8/8 : RUN mkdir -p "$JAVA_HOME"; tar --extract --file /usr/local/$JDK_FILE --directory "$JAVA_HOME" --strip-components 1 --no-same-owner; rm /usr/local/$JDK_FILE ---> Running in a63663306adc Removing intermediate container a63663306adc ---> ddc652d5dec0 Successfully built ddc652d5dec0 Successfully tagged bolingcavalry/arm64jdk:8
驗(yàn)證一下效果:
root@raspbian:~/test# docker run --rm bolingcavalry/arm64jdk:8 java -version java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
將剛剛做好的鏡像推送到倉(cāng)庫(kù),這么做的原因如下:
可以讓大家使用到此鏡像;
接下來(lái)要用Jib插件將Java應(yīng)用制作成鏡像,Jib插件一定要從鏡像倉(cāng)庫(kù)下載<font color="blue">bolingcavalry/arm64jdk:8</font>作為Java應(yīng)用的基礎(chǔ)鏡像;
這里我將<font color="blue">bolingcavalry/arm64jdk:8</font>推送到了hub.docker.com,如果您沒(méi)有hub.docker.com的賬號(hào),也可以選擇推送到私有鏡像倉(cāng)庫(kù),只要是鏡像倉(cāng)庫(kù),Jib插件都支持;
以前面提到的hellojib為例,打開(kāi)pom.xml文件,將jib插件的配置改為如下內(nèi)容:
<!--使用jib插件--> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>1.3.0</version> <configuration> <!--from節(jié)點(diǎn)用來(lái)設(shè)置鏡像的基礎(chǔ)鏡像,相當(dāng)于Docerkfile中的FROM關(guān)鍵字--> <from> <!--使用openjdk官方鏡像,tag是8-jdk-stretch,表示鏡像的操作系統(tǒng)是debian9,裝好了jdk8--> <image>bolingcavalry/arm64jdk:8</image> </from> <to> <!--鏡像名稱(chēng)和tag,使用了mvn內(nèi)置變量${project.version},表示當(dāng)前工程的version--> <image>bolingcavalry/hellojib:${project.version}</image> </to> <!--容器相關(guān)的屬性--> <container> <!--jvm內(nèi)存參數(shù)--> <jvmFlags> <jvmFlag>-Xms1g</jvmFlag> <jvmFlag>-Xmx1g</jvmFlag> </jvmFlags> <!--要暴露的端口--> <ports> <port>8080</port> </ports> </container> </configuration> </plugin>
執(zhí)行命令<font color="blue">mvn clean compile jib:dockerBui-U</font>即可構(gòu)建鏡像,控制臺(tái)輸出如下信息(友情提示,這可能是個(gè)漫長(zhǎng)的等待過(guò)程,我這等了9分多鐘):
root@raspbian:~/test/hellojib# mvn clean compile jib:dockerBuild -U [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.bolingcavalry:hellojib >--------------------- [INFO] Building hellojib 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hellojib --- [INFO] Deleting /root/test/hellojib/target [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ hellojib --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hellojib --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /root/test/hellojib/target/classes [INFO] [INFO] --- jib-maven-plugin:1.3.0:dockerBuild (default-cli) @ hellojib --- [INFO] [INFO] Containerizing application to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT... [INFO] The base image requires auth. Trying again for bolingcavalry/arm64jdk:8... [INFO] [INFO] Container entrypoint set to [java, -Xms1g, -Xmx1g, -cp, /app/resources:/app/classes:/app/libs/*, com.bolingcavalry.hellojib.HellojibApplication] [INFO] [INFO] Built image to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT [INFO] Executing tasks: [INFO] [==============================] 100.0% complete [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 09:59 min [INFO] Finished at: 2019-10-02T19:42:42+08:00 [INFO] ------------------------------------------------------------------------
驗(yàn)證hellojib工程的鏡像是否正常,執(zhí)行命令<font color="blue">docker run --rm -p 8080:8080 bolingcavalry/hellojib:0.0.1-SNAPSHOT</font>,控制臺(tái)顯示SpringBoot應(yīng)用啟動(dòng)成功:
root@raspbian:~/test/hellojib# docker run --rm -p 8080:8080 bolingcavalry/hellojib:0.0.1-SNAPSHOT . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) 2019-10-02 11:45:02.425 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : Starting HellojibApplication on d2d856d3c623 with PID 1 (/app/classes started by root in /) 2019-10-02 11:45:02.440 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : No active profile set, falling back to default profiles: default 2019-10-02 11:45:07.203 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-10-02 11:45:07.342 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-10-02 11:45:07.344 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21] 2019-10-02 11:45:07.761 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-10-02 11:45:07.762 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5108 ms 2019-10-02 11:45:08.863 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-10-02 11:45:10.027 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-10-02 11:45:10.037 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : Started HellojibApplication in 8.932 seconds (JVM running for 9.876) 2019-10-02 11:45:52.965 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-10-02 11:45:52.966 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-10-02 11:45:52.993 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 27 ms
ARM電腦的IP地址是<font color="blue">192.168.50.118</font>,因此在瀏覽器訪(fǎng)問(wèn):http://192.168.50.118:8080/hello ,如下圖,可見(jiàn)hellojib工程的容器可以正常工作,成功返回了數(shù)據(jù): SpringBoot工程終于在ARM機(jī)器的Docker環(huán)境下成功運(yùn)行了,這里采用的是自制JDK8鏡像的方式,還有一種方法也是可行的,即:使用OpenJDK官方的JDK11鏡像;
使用JDK11鏡像,意味著Java工程所用的JDK從8升級(jí)到11,這個(gè)操作和Docker的關(guān)系不大,您只要驗(yàn)證應(yīng)用在升級(jí)JDK后是否能運(yùn)行正常即可,本文就不贅述了,我把自己在升級(jí)過(guò)程中遇到的問(wèn)題列出來(lái),幫您跳過(guò)小坑:
從JDK9開(kāi)始引入了module的概念,JDK8自帶的一些jar包不再默認(rèn)提供,您需要在應(yīng)用的pom.xml中添加以下依賴(lài),否則SpringBoot啟動(dòng)時(shí)會(huì)因?yàn)槟承﹍ass找不到導(dǎo)致啟動(dòng)失敗:
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.1</version> </dependency>
如果您的不想修改pom.xml,此時(shí)我的做法: a. 將上述三個(gè)依賴(lài)對(duì)應(yīng)的jar包全部找出來(lái)(注意這里絕不止三個(gè)jar包,還有它們的間接依賴(lài)),放在ARM電腦的某個(gè)文件夾下面,例如/usr/local/extendJar; b. 修改Jib插件的配置,增加一個(gè)classpath,例如:/usr/local/extendJar(注意這里的路徑是容器內(nèi)的); c. 在啟動(dòng)容器的時(shí)候,增加一個(gè)數(shù)據(jù)卷映射,將宿主機(jī)的/usr/local/extendJar映射到容器的/usr/local/extendJar;
上述就是小編為大家分享的ARM架構(gòu)下的Docker環(huán)境沒(méi)有OpenJDK=8版本鏡像該如何解決了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。