前提:
1.gradle已经安装,环境变量已配置,命令行可以正常使用
2.windows版本docker工具已安装,docker命令可以正常使用
3.docker hub 账号已经注册 :https://cloud.docker.com
第一步:
编写gradle项目(我写的是一个简单的springcloud eureka注册中心)
第二步:
注意:
//添加gradle-docker 依赖,版本1.2
classpath 'se.transmode.gradle:gradle-docker:1.2'
添加gradle docker插件
编写gradle.build
buildscript {
ext {
springBootVersion = '2.1.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
//添加gradle-docker 依赖,版本1.2
classpath 'se.transmode.gradle:gradle-docker:1.2'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
//apply plugin: 'application' //可选配置
apply plugin: 'docker'
group = '你的docker hub账户名'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
jar {
baseName = 'springboot-gradle'
version = '0.0.1-SNAPSHOT'
}
docker {
baseImage 'store/oracle/serverjre:8'
maintainer 'kebencool kebencool@keben'
registry '你要命名的仓库'
}
task dockerBuilder(type: Docker) {
applicationName = jar.baseName
tagVersion = jar.version
volume('/tmp')
//发布到远程docker hub仓库
push = project.hasProperty('push')
addFile("${jar.baseName}-${jar.version}.jar","app.jar")
entryPoint(["java","-Djava.security.egd=file:/dev/./urandom","-jar",'app.jar'])
exposePort(8761)
doFirst {
copy {
from jar
into stageDir
}
}
}
repositories {
mavenLocal()
//使用阿里云镜像
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}
ext {
set('springCloudVersion', 'Greenwich.RELEASE')
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server:2.1.0.RELEASE'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
第三步:
1.在IDEA命令行输入:
gradle build
如果报错可能是gradle环境变量有问题,或者是gradle.build文件内容有问题,详情可以直接复制报错信息google
2.构建完成后,目录下会有jar包
build>libs>%baseName %-%version %.jar
3.在命令行输入
java -jar build/libs/你的包名.jar
4.在浏览器检查你的项目有没有正常跑起来,如果有则打包成功,接下来需要把这个包打包成镜像(在没有添加
push = project.hasProperty('push')前,镜像还在本地)
命令行输入
gradle build dockerBuilder
注意dockerBuilder是task名,跟方法名类似,以自己的为准
5.生成镜像成功后,添加push = project.hasProperty('push') 上传到远程docker hub
命令行输入
gradle dockerBuilder -Ppush
此时我出现了该问题:
> Docker execution failed
Command line [docker push com.example/springboot-gradle:0.0.1-SNAPSHOT] returned:
Get https://com.example/v2/: dial tcp: lookup com.example on 192.168.65.1:53: no such host
原因是:https://com.example/v2/ ,一开始我的gradle.build文件中group属性命名为com.example,跟我的docker hub帐户名不符,所以上传失败,可以修改group熟悉,也可以直接用docker tag命令修改已经创建好的镜像名前缀。
重新运行gradle dockerBuilder -Ppush,上传成功,但是没有配国外加速所以上传有点慢。
问题:
* What went wrong:
Execution failed for task ':dockerBuilder'.
> Docker execution failed
Command line [docker push kebencool/config-server:0.0.2-SNAPSHOT] returned:
Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 17s
1 actionable task: 1 executed
解决:本地没有关闭防火墙或者本地docker工具没有运行,可以试试restart