SpringBoot 构建 Docker 镜像的 gradle 有3 种方式
本文将介绍3种技术,通过 maven 把 SpringBoot 应用构建成 Docker 镜像。
(1)使用 spring-boot-gradle-plugin 内置的 build-image .
(2)使用 Google 的 jib-gradle-plugin 。
(3)使用 dockerfle-gradle-plugin 。
这里用的是 jib-gradle-plugin
官网地址
核心配置
id 'com.google.cloud.tools.jib' version '3.1.4'
jib {
// from {
// image = 'registry.cn-shanghai.aliyuncs.com/starxin/docker-jib:1.0'
// }
to {
image = 'registry.cn-hangzhou.aliyuncs.com/chenye-repository/test-springboot-image'
auth {
username = "chenye15002770045"
password = "3451"
}
}
container {
creationTime = 'USE_CURRENT_TIMESTAMP'
jvm Flags = ['-Djava.security.egd=file:/dev/./urandom', '-Dspring.profiles.active=prod', '-Dfile.encoding=utf-8', '-Duser.timezone=GMT+08']
mainClass = 'com.example.bootdockerdemo.BootDockerDemoApplication'
ports = ['8080']
}
}
g
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'org.springframework.experimental.aot' version '0.10.5'
id 'org.graalvm.buildtools.native' version '0.9.4'
// id "com.google.cloud.tools.jib" version "2.0.0"
id 'com.google.cloud.tools.jib' version '3.1.4'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extends From annotationProcessor
}
}
repositories {
maven { url '#39; }
maven { url '#39; }
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
// developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
bootBuildImage {
builder = 'paketobuildpacks/builder:tiny'
builder = 'registry.cn-hangzhou.aliyuncs.com/chenye-repository/test-springboot-image:1.0'
environment = ['BP_NATIVE_IMAGE': 'true']
// host ='tcp://192.168.99.100:2376'
publish = true
}
nativeBuild {
classpath processAotResources.outputs, compileAotJava.outputs
}
nativeTest {
classpath processAotTestResources.outputs, compileAotTestJava.outputs
}
jib {
// from {
// image = 'registry.cn-shanghai.aliyuncs.com/starxin/docker-jib:1.0'
// }
to {
image = 'registry.cn-hangzhou.aliyuncs.com/chenye-repository/test-springboot-image'
auth {
username = "chenye15002770045"
password = "3451"
}
}
container {
creationTime = 'USE_CURRENT_TIMESTAMP'
jvmFlags = ['-Djava.security.egd=file:/dev/./urandom', '-Dspring.profiles.active=prod', '-Dfile.encoding=utf-8', '-Duser.timezone=GMT+08']
mainClass = 'com.example.bootdockerdemo.BootDockerDemoApplication'
ports = ['8080']
}
}