gradle 集成 dockerFile

gradle 集成 dockerFile

gradle 是一个新兴的jvm编译工具,docker也是最流行的容器部署方案,docker传统通过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不是很多,但是剩在简单粗暴,可以让我继续
//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上搜索下,只是这么一来,就会比较复杂点。

    原文作者:来福马斯特
    原文地址: https://www.jianshu.com/p/3cd9dbc165e9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞