##1、简介
Jib是Google开源的镜像打包工具,以Maven和Gradle插件的形式提供,用来将你的Java应用程序制作为Docker镜像文件。GitHub地址:https://github.com/GoogleContainerTools/jib
plugins {
id 'java'
id 'com.google.cloud.tools.jib' version '0.9.11'
}
group 'com.dameng.dmcp'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.46'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jib {
container {
mainClass = "com.dameng.dmcp.tools.DatabaseTools"
useCurrentTimestamp = true
jvmFlags = ['-Dfile.encoding=UTF-8']
}
from {
image = '192.168.101.88:5000/distroless/java:latest'
}
to {
image = "192.168.101.88:5000/dmcop2/${project.name}:${version}"
}
}
如上所示,使用非常简单。以Gradle为例,引入插件,定义Jib相关属性即可,Jib会引入相关的task。详细属性参见:https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin
1、Connect to gcr.io/108.177.97.82:443 timed out
在没有from属性的情况,默认使用gcr.io/distroless/java
镜像文件作为基础进行,常规情况下gcr.io显然无法访问
目前Jib***无法***使用本地镜像文件作为基础镜像,也就是说,Jib在每次构建时都会重新下载基础镜像文件
2、生成的镜像时间显示:48 years ago
默认情况下,Jib为了保留再生性(reproducibility)擦除了所有时间戳,通过设置 useCurrentTimestamp = true
以当前时间作为创建时间
I don’t know what is reproducibility~
3、使用启用TLS的Docker Registry作为基础镜像来源
Jib在拉取基础镜像文件时,使用jdk的cacerts中的CA来验证https请求释放可信,在Docker Registry使用自签名的CA来启用TLS时,需要在maven插件或者gradle插件中设置证书可信,设置方式有两种,主要介绍第二种:
使用KeyStore Explorer将自签名证书转换为jks,通过jvm参数指定jks文件,详细步骤参考:https://github.com/GoogleContainerTools/jib/blob/master/docs/self_sign_cert.md,jvm参数:-Djavax.net.ssl.trustStorePassword=password -Djavax.net.ssl.trustStore=/path/to/keystore.jks
将自签名CA导入到JDK的cacerts文件中,步骤如下:
### 进入jdk证书文件存放路径 ###
chenleis-MacBook-Pro:~ chenlei$ cd /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/security
chenleis-MacBook-Pro:security chenlei$ ll
total 344
-rw-r--r-- 1 root wheel 4054 Jul 7 16:21 blacklist
-rw-r--r-- 1 root wheel 1273 Jul 7 16:21 blacklisted.certs
-rw-r--r-- 1 root wheel 115649 Nov 21 10:10 cacerts
-rw-r--r-- 1 root wheel 2466 Jul 7 16:21 java.policy
-rw-r--r-- 1 root wheel 41565 Jul 7 16:21 java.security
drwxr-xr-x 4 root wheel 128 Jul 7 16:21 policy
-rw-r--r-- 1 root wheel 0 Jul 7 16:21 trusted.libraries
### 导入证书,cacerts默认密码:changeit,-file参数指定自签名证书路径 ###
chenleis-MacBook-Pro:security chenlei$ sudo keytool -import -keystore cacerts -storetype JKS -storepass changeit -file /tmp/ca.crt -alias dameng
### 查看cacerts内容 ###
chenleis-MacBook-Pro:security chenlei$ keytool -list -keystore cacerts -storetype JKS -storepass changeit
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 106 entries
verisignclass2g2ca [jdk], Aug 25, 2016, trustedCertEntry,
Certificate fingerprint (SHA1): B3:EA:C4:47:76:C9:C8:1C:EA:F2:9D:95:B6:CC:A0:08:1B:67:EC:9D
。。。。。。
### 从cacerts移除证书 ###
chenleis-MacBook-Pro:security chenlei$ sudo keytool -delete -alias dameng -keystore cacerts -storetype JKS -storepass changeit
4、镜像中文乱码
默认基础镜像 distroless/java:latest
中的locale不支持中文,通过设置 jvmFlags = ['-Dfile.encoding=UTF-8']
可绕开该问题,参考:https://github.com/GoogleContainerTools/distroless/issues/253