欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):
https://github.com/zq2599/blog_demos
为什么需要ARM架构下的OpenJDK8的Docker镜像?
- 对现有的Java应用,之前一直运行在x86处理器环境下,编译和运行都是JDK8,如今在树莓派的Docker环境运行(或者其他ARM架构电脑,例如华为泰山服务器),需要JDK8镜像作为基础镜像。
现在有什么问题?
应对之道
- 自己编译一个8版本的OpenJDK安装包,以此来做Docker镜像;
- Oracle提供了ARM版本的JDKD安装包,以此包来做Docker镜像;
- 用OpenJDK的11版本,但是11和8的差异要自行处理;
- 对于第一种方式,自己编译8版本的OpenJDK,难度太大(对我自己而言),因为编译OpenJDK需要低版本的OpenJDK作为编译工具,也就是说我要找到ARM版本的OpenJDK7,才能编译ARM版本的OpenJDK8,因此我觉得这样做的难度太大…
- 今天要讨论的是第二种和第三种,
环境信息
- 硬件:树莓派4B
- 操作系统:openfans的64为Debian
- Docker:19.03.1
- docker-compose:1.24.1
参考文档
Java应用的源码
- 本文要解决的问题是ARM架构的电脑上,如何在Docker环境运行Java应用,因此需要有个Java应用来验证,这里找了个最普通的SpringBoot应用,提供一个hello world的http接口,通过jib插件构建成Docker镜像,整个应用的源码可以从GitHub上下载,地址和链接信息如下表所示:
- 这个git项目中有多个文件夹,本章的源码在hellojib文件夹下,如下图红框所示:
操作步骤简介
ARM机器上安装JDK
- 上述JDK文件解压后是个名为jdk1.8.0_221的文件夹,将此文件夹放在ARM电脑的/usr/lib/jvm目录下;
- 打开文件~/.bashrc,增加以下内容:
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
- 执行source ~/.bashrc,使得配置立即生效;
- 执行命令java -version试试JDK是否已经可用:
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)
ARM机器上安装Maven
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=$MAVEN_HOME/bin:$PATH
- 执行source ~/.bashrc,使得配置立即生效;
- 执行命令mvn -version试试maven是否已经可用:
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: "aarch64", family: "unix"
- 折腾了这么久,总算是完成了准备工作,接下来开始做作镜像了;
- 要把咱们自己的Java应用做成Docker镜像,需要有个JDK8镜像作为基础镜像,接下来我们来做这个基础镜像;
自己动手,做一个JDK8镜像
- 在ARM电脑上新建一个文件夹,里面新建名为Dockerfile的文件,内容如下:
#Docker image of JDK8 in ARM64
# VERSION 8
# Author: bolingcavalry
#基础镜像使用的是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
- 将之前下载的jdk-8u221-linux-arm64-vfp-hflt.tar.gz文件复制到Dockerfile文件所在目录;
- 在Dockerfile文件所在目录执行命令docker build -t bolingcavalry/arm64jdk:8 .(注意命令的末尾有个小数点,不要漏了)
- 执行成功后控制台输出以下信息:
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
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)
把镜像推送到镜像仓库
- 将刚刚做好的镜像推送到仓库,这么做的原因如下:
- 可以让大家使用到此镜像;
- 接下来要用Jib插件将Java应用制作成镜像,Jib插件一定要从镜像仓库下载bolingcavalry/arm64jdk:8作为Java应用的基础镜像;
- 这里我将bolingcavalry/arm64jdk:8推送到了hub.docker.com,如果您没有hub.docker.com的账号,也可以选择推送到私有镜像仓库,只要是镜像仓库,Jib插件都支持;
将Java应用构建成镜像
- 以前面提到的hellojib为例,打开pom.xml文件,将jib插件的配置改为如下内容:
<!--使用jib插件-->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
<from>
<!--使用openjdk官方镜像,tag是8-jdk-stretch,表示镜像的操作系统是debian9,装好了jdk8-->
<image>bolingcavalry/arm64jdk:8</image>
</from>
<to>
<!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->
<image>bolingcavalry/hellojib:${project.version}</image>
</to>
<!--容器相关的属性-->
<container>
<!--jvm内存参数-->
<jvmFlags>
<jvmFlag>-Xms1g</jvmFlag>
<jvmFlag>-Xmx1g</jvmFlag>
</jvmFlags>
<!--要暴露的端口-->
<ports>
<port>8080</port>
</ports>
</container>
</configuration>
</plugin>
- 执行命令mvn clean compile jib:dockerBui-U即可构建镜像,控制台输出如下信息(友情提示,这可能是个漫长的等待过程,我这等了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] ------------------------------------------------------------------------
- 验证hellojib工程的镜像是否正常,执行命令docker run --rm -p 8080:8080 bolingcavalry/hellojib:0.0.1-SNAPSHOT,控制台显示SpringBoot应用启动成功:
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
- SpringBoot工程终于在ARM机器的Docker环境下成功运行了,这里采用的是自制JDK8镜像的方式,还有一种方法也是可行的,即:使用OpenJDK官方的JDK11镜像;
使用OpenJDK官方的JDK11镜像
- 使用JDK11镜像,意味着Java工程所用的JDK从8升级到11,这个操作和Docker的关系不大,您只要验证应用在升级JDK后是否能运行正常即可,本文就不赘述了,我把自己在升级过程中遇到的问题列出来,帮您跳过小坑:
- 从JDK9开始引入了module的概念,JDK8自带的一些jar包不再默认提供,您需要在应用的pom.xml中添加以下依赖,否则SpringBoot启动时会因为某些lass找不到导致启动失败:
<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,此时我的做法:
a. 将上述三个依赖对应的jar包全部找出来(注意这里绝不止三个jar包,还有它们的间接依赖),放在ARM电脑的某个文件夹下面,例如/usr/local/extendJar;
b. 修改Jib插件的配置,增加一个classpath,例如:/usr/local/extendJar(注意这里的路径是容器内的);
c. 在启动容器的时候,增加一个数据卷映射,将宿主机的/usr/local/extendJar映射到容器的/usr/local/extendJar;
- 至此,OpenJDK官方在ARM架构不提供8版本镜像的问题已完美解决,如果您正在使用ARM服务器做Docker+Java开发,希望此文能给您一些参考。
欢迎关注阿里云开发者社区博客:程序员欣宸
学习路上,你不孤单,欣宸原创一路相伴...