ARM 架构下的 Docker 环境,OpenJDK 官方没有 8 版本镜像,如何完美解决?
去 Oracle 网站下载 ARM 版本的 JDK8,地址是:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ,如下图红框所示:
上述 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=.:{JRE_HOME}/lib
export PATH=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
去 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
安装包解压之后是个名为 apache-maven-3.6.2 的文件夹,将此文件夹放在 ARM 电脑的/usr/local 目录下;
打开文件~/.bashrc,增加以下内容:
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=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 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 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 "JDK_FILE --directory "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 插件的配置改为如下内容:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<from>
<image>bolingcavalry/arm64jdk:8</image>
</from>
<to>
<image>bolingcavalry/hellojib:${project.version}</image>
</to>
<container>
<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]
[I
NFO] 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 /)
评论