gradle 集成 dockerFile
gradle 是一個新興的jvm編譯工具蕾额,docker也是最流行的容器部署方案赖钞,docker傳統(tǒng)通過Dockerfile進行image的打包丰涉,這時候我們想要比較方便的結合docker和gradle
就是在gradle編譯的時候可能進行docker的打包硫椰。就可以使用一個gradle的plugin
build.gradle
buildscript{
repositories{
mavenCentral()
}
dependencies{
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
}
}
//這里就是必須依賴的docker的gradle plugin闸度,相比而言洪规,這個plugin雖然star不是很多,但是剩在簡單粗暴醇份,可以讓我繼續(xù)
//dockerfile進行撰寫Docker的命令
plugins {
id "org.sglahn.gradle-dockerfile-plugin" version "0.4"
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
version='0.0.2'
jar{
baseName = 'gs-sb-docker'
version = '0.0.1'
}
repositories{
mavenCentral()
}
sourceSets{
main{
java{
srcDirs = []
}
groovy{
srcDirs = ['src/main/groovy','src/main/java']
}
test{
java{
srcDirs = []
}
groovy{
srcDirs = ['src/test/groovy','src/test/java']
}
}
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies{
compile("org.springframework.boot:spring-boot-starter-web")
compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.4'
testCompile("org.springframework.boot:spring-boot-starter-test")
}
//configuration
docker {
// Image version. Optional, default = project.version
//imageVersion = version
// Image name. Optional, default = project.name
imageName = 'sbdocker'
// Docker repository. Optional, default == no repository
// dockerRepository = 'sglahn'
// Path or URL referring to the build context. Optional, default = ${project.projectDir.getAbsolutePath()}
// buildContext = 'build-context'
// Path to the Dockerfile to use (relative to ${project.projectDir}). Optional, default = ${buildContext}/Dockerfile
dockerFile = 'src/main/docker/Dockerfile'
// Add a list of tags for an image. Optional, default = 'latest'
//tags = [version, 'latest', 'Hello']
// Set metadata for an image. Optional, default = no label applied
//labels = ['branch=master', 'mylabel=test']
// name and value of a buildarg. Optional, default = no build arguments
//buildArgs = ['http_proxy="http://some.proxy.url"']
// Always remove intermediate containers, even after unsuccessful builds. Optional, default = false
removeIntermediateContainers = true
// Isolation specifies the type of isolation technology used by containers. Optional, default = default
//isolation = 'default'
// Do not use cache when building the image. Optional, default = false
//noCache = true
// Always attempt to pull a newer version of the image. Optional, default false
//pull = true
// Suppress the build output and print image ID on success. Optional, default = true
quiet = false
// Remove image in local repository after push to a remote repository, useful for builds on CI agents. Optional, default = false
//removeImagesAfterPush = true
}
cat src/main/docker/Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD build/libs/gs-sb-docker-0.0.1.jar /app.jar
ENV JAVA_OPTS=""
CMD ["java","-jar","/app.jar"]
以上就是最簡單的docker和gradle的結合了稼锅。
如果想要徹底不寫Dockerfile,改用其他的gradle plugin也是可以的僚纷,大家可以去github上搜索下矩距,只是這么一來,就會比較復雜點怖竭。