Gradle打包Springboot应用为镜像并且上传到私服仓库

843 阅读1分钟

Gradle打包springboot应用为镜像上传到镜像仓库

在使用springboot经常打包jar包,又或者我们通过手动执行docker build来将dockerfile打包成为镜像push到仓库中,这里结合gradle-docker-plugin插件来构建/推送镜像。 github.com/bmuschko/gr…

gradle插件配置和dockerfile

插件版本
gradle 5.2.1
com.bmuschko.docker-remote-api 6.7.0

plugins {
    id 'com.bmuschko.docker-remote-api' version '6.7.0'
}

import com.bmuschko.gradle.docker.tasks.image.*

//基于dockerfile构建镜像,指定镜像tag通过inputDir指定dockerfile位置
task buildMyAppImage(type: DockerBuildImage) {
    inputDir = file('./')
    images.add('reg.xxx.com/smartos/sync-tcs-harbo:0.01')
}

//指定镜像和仓库地址,用户名上传镜像
task pushMyAppImage(type: DockerPushImage){
    images.add('reg.xxx.com/smartos/sync-tcs-harbo:0.01')
    registryCredentials {
        url = 'http://reg.xxx.com'
        username = 'xxx'
        password = '***.com'
        email = 'xxx@xxx.com'
    }
}

dockerfile

FROM reg.xxx.com/library/java:8u111-jdk
WORKDIR /app
ADD build/libs/sync-tcs-harbo.jar /app
ENV app=smartos-tcs-sync-service
ENV TZ=Asia/Shanghai
ENTRYPOINT ["java", "-jar", "sync-tcs-harbo.jar"]

执行任务构建镜像

➜  sync-tcs git:(feature_gradle_docker) ✗ ./gradlew buildMyAppImage

> Task :buildMyAppImage
Building image using context '/Users/qujianfei/code/smartos/sync-tcs'.
Using images 'reg.xxx.com/smartos/sync-tcs-harbo:0.02'.

Step 1/6 : FROM reg.xxx.com/library/java:8u111-jdk
 ---> 4cb3eefd2af3
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> 81fd3a4db707
Step 3/6 : ADD build/libs/sync-tcs-harbo.jar /app
 ---> 527841793339
Step 4/6 : ENV app=smartos-tcs-sync-service
 ---> Running in 62a20390e66c
Removing intermediate container 62a20390e66c
 ---> e02d0bfdd868
Step 5/6 : ENV TZ=Asia/Shanghai
 ---> Running in 65108e3d790f
Removing intermediate container 65108e3d790f
 ---> f9c2e30de2af
Step 6/6 : ENTRYPOINT ["java", "-jar", "sync-tcs-harbo.jar"]
 ---> Running in 704d277b36f0
Removing intermediate container 704d277b36f0
 ---> 8aef0ccd77d4
Successfully built 8aef0ccd77d4
Successfully tagged reg.xxx.com/smartos/sync-tcs-harbo:0.02

> Task :buildMyAppImage
Created image with ID '8aef0ccd77d4'.

BUILD SUCCESSFUL in 46s
1 actionable task: 1 executed

生成本地镜像 image.png

执行任务推送镜像到仓库

➜  sync-tcs git:(feature_gradle_docker) ✗ ./gradlew pushMyAppImage
> Task :pushMyAppImage
Pushing image 'reg.xxx.com/smartos/sync-tcs-harbo:0.02'.
BUILD SUCCESSFUL in 1m 4s
1 actionable task: 1 executed

镜像上传成功 image.png

总结

使用gradle-docker-plugin插件,可以通过gradle执行任务来完成镜像的构建和推送。