Spring Boot 專案 透過gradle 打包成 docker 專案 一步一步 教學
Spring Boot 專案 透過gradle 打包成 docker 專案 一步一步 教學
先透過 gradle 建立 Spring boot 專案
這邊網路有很多資源直接參考別人的
先來修改 build.gradle
//在 plugin 區塊
plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id "com.palantir.docker" version "0.20.1" //<---- com.palantir.docker="" span="">
}
apply plugin: "com.palantir.docker" // <-- apply="" com.palantir.docker="" span="">
...
// 在最後增加兩個
task unpack(type: Copy) {
dependsOn bootJar
from(zipTree(tasks.bootJar.outputs.files.singleFile))
into("build/dependency")
}
docker {
name "${project.group}/${bootJar.baseName}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
-->---->
全部如下
plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id "com.palantir.docker" version "0.20.1"
}
apply plugin: "com.palantir.docker"
group = 'com.domi'
version = '0.0.35'
ext {
set('springBootAdminVersion', "2.2.2")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation "org.springframework.boot:spring-boot-starter-security"
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation "org.springframework:spring-context"
implementation 'org.springframework:spring-context-support'
implementation 'org.springframework.boot:spring-boot-starter-quartz'
implementation 'com.microsoft.azure:azure-documentdb:2.4.6'
}
springBoot {
buildInfo()
}
task unpack(type: Copy) {
dependsOn bootJar
from(zipTree(tasks.bootJar.outputs.files.singleFile))
into("build/dependency")
}
docker {
name "${project.group}/${bootJar.baseName}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
增加 DockerFile 在專案目錄下
放在跟 build.gradle 同一層 ,檔名為 Dockerfile
內容如下
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
# example com.sun.test.Application
ENTRYPOINT ["java","-Denv=prod","-cp","app:app/lib/*","{這邊換成Spring boot main class}",">","/tmp/console.log"]
如何執行
打包成 image
gradle docker
其他的方法
docker - Builds Docker image.
dockerClean - Cleans Docker build directory.
dockerfileZip - Bundles the configured Dockerfile in a zip file
dockerPrepare - Prepares Docker build directory.
dockerPush - Pushes named Docker image to configured Docker Hub.
dockerTag - Applies all tags to the Docker image.
md code:
留言
張貼留言