Docker environment of the ARM architecture, OpenJDK 8 no official version of the mirror, how perfect solution?

Why Docker mirror OpenJDK8 under the ARM architecture?

Existing Java application, until running in x86 processor environment, compile and run all JDK8, now in Docker environment running raspberry pie (or other ARM architecture computers, such as Huawei Tarzan server), as required JDK8 mirror base image.

Now what is the problem?

In the next "ARM64 architecture, OpenJDK official version 8 Docker mirror why not? " One article, has identified 8 OpenJDK does not provide official version of Docker image, therefore, the original Java application, if it is based on JDK8 compile and run, and now he moved from X86 architecture to the next Docker environment ARM architecture, will faced no problems JDK base image;

Response to it

"Under ARM64 architecture, OpenJDK official version 8 Docker mirror why not? " The article mentioned response to it:

  1. To compile a version of the OpenJDK 8 installation package, in order to do Docker mirror;
  2. Oracle provides the ARM version of JDKD installation package, this package do Docker mirror;
  3. But the differences were 11 and 8 to be treated with their own version of the OpenJDK 11;

For the first way, compile their own version of the OpenJDK 8, too difficult (For myself), because the compiler needs low OpenJDK version of OpenJDK as the compilation tools, which means I have to find ARM version of OpenJDK7, to compile ARM version of OpenJDK8, so I think that the difficulty of doing too much ...

Today to discuss is the second and the third,

Environmental Information

  1. Hardware: Raspberry Pi 4B
  2. Operating System: openfans 64 for Debian
  3. Docker:19.03.1
  4. docker-compose:1.24.1

Reference Documents

  1. In the Raspberry Pi 4B install 64-bit Debian and Docker method, please refer to the "Raspberry Pi 4B install 64-bit Linux (without monitor keyboard and mouse)."
  2. In the process of installing 4B raspberry pie docker-compose, refer to "Raspberry Pi 4B install docker-compose (64 Wei Linux)"
  3. Java applications will be made into a Docker image, refer to "Docker and Jib (maven plug-in version) combat."

Source Java application

In this paper to solve the problem is on the ARM architecture computer, how Docker environment to run Java applications, it is necessary to have a Java application to verify, to find the most common SpringBoot applications, providing a hello world of http interface through jib plug Docker constructed image, the entire application source code can be downloaded from GitHub, address and link information in the following table:

name link Remark
Project Home https://github.com/zq2599/blog_demos The project's home page on GitHub
git repository address (https) https://github.com/zq2599/blog_demos.git The project's source code repository address, https protocol
git repository address (ssh) [email protected]:zq2599/blog_demos.git The project's source code repository address, ssh protocol

The git item has multiple folders, folder in this chapter hellojib source file, as shown in FIG red box:
Here Insert Picture Description

About Procedure

The next steps, as shown below:
Here Insert Picture Description

JDK machine mounted on ARM

To compile on ARM machines hellojib build the project, it is imperative JDK and Maven installed, first install JDK;

  1. Oracle Web site to download the ARM version of JDK8, the address is: https: //www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, red box shown in the following figure:
    Here Insert Picture Description
  2. 上述JDK文件解压后是个名为jdk1.8.0_221的文件夹,将此文件夹放在ARM电脑的/usr/lib/jvm目录下;
  3. 打开文件~/.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
  1. 执行source ~/.bashrc,使得配置立即生效;
  2. 执行命令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

  1. 去Maven官网下载安装包,我这里下载的是3.6.2版本,地址:https://www-eu.apache.org/dist/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
  2. 安装包解压之后是个名为apache-maven-3.6.2的文件夹,将此文件夹放在ARM电脑的/usr/local目录下;
  3. 打开文件~/.bashrc,增加以下内容:
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=$MAVEN_HOME/bin:$PATH
  1. 执行source ~/.bashrc,使得配置立即生效;
  2. 执行命令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镜像

  1. 在ARM电脑上新建一个文件夹,里面新建名为Dockerfile的文件,内容如下:
#Docker image of JDK8 in ARM64
# VERSION 8
# Author: bolingcavalry

#基础镜像使用的是OpenJDK官方镜像公用的
FROM buildpack-deps:stretch-scm

#作者
MAINTAINER BolingCavalry <[email protected]>

# 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
  1. 将之前下载的jdk-8u221-linux-arm64-vfp-hflt.tar.gz文件复制到Dockerfile文件所在目录;
  2. 在Dockerfile文件所在目录执行命令docker build -t bolingcavalry/arm64jdk:8 .(注意命令的末尾有个小数点,不要漏了)
  3. 执行成功后控制台输出以下信息:
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 <[email protected]>
 ---> 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
  1. 验证一下效果:
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)

把镜像推送到镜像仓库

将刚刚做好的镜像推送到仓库,这么做的原因如下:

  1. 可以让大家使用到此镜像;
  2. 接下来要用Jib插件将Java应用制作成镜像,Jib插件一定要从镜像仓库下载bolingcavalry/arm64jdk:8作为Java应用的基础镜像;

这里我将bolingcavalry/arm64jdk:8推送到了hub.docker.com,如果您没有hub.docker.com的账号,也可以选择推送到私有镜像仓库,只要是镜像仓库,Jib插件都支持;

将Java应用构建成镜像

  1. 以前面提到的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>
  1. 执行命令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] ------------------------------------------------------------------------
  1. 验证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
  1. ARM电脑的IP地址是192.168.50.118,因此在浏览器访问:http://192.168.50.118:8080/hello ,如下图,可见hellojib工程的容器可以正常工作,成功返回了数据:
    Here Insert Picture Description
    SpringBoot工程终于在ARM机器的Docker环境下成功运行了,这里采用的是自制JDK8镜像的方式,还有一种方法也是可行的,即:使用OpenJDK官方的JDK11镜像;

使用OpenJDK官方的JDK11镜像

使用JDK11镜像,意味着Java工程所用的JDK从8升级到11,这个操作和Docker的关系不大,您只要验证应用在升级JDK后是否能运行正常即可,本文就不赘述了,我把自己在升级过程中遇到的问题列出来,帮您跳过小坑:

  1. 从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>
  1. 如果您的不想修改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开发,希望此文能给您一些参考。

Published 328 original articles · won praise 946 · Views 1.17 million +

Guess you like

Origin blog.csdn.net/boling_cavalry/article/details/101855126