美文网首页
gradle docker 项目构建

gradle docker 项目构建

作者: bullion | 来源:发表于2018-09-13 13:57 被阅读0次

    docker方式

    创建并实现项目(此处是使用配置中心项目做案例):

    修改build.gradle文件(使用的docker为阿里云镜像服务):

    buildscript {

        ext {

            springBootVersion = '2.0.3.RELEASE'

        }

        repositories {

            mavenCentral()

            maven { url "https://plugins.gradle.org/m2/" }

        }

        dependencies {

            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

            classpath('se.transmode.gradle:gradle-docker:1.2') //Build a Docker Image with Gradle

        }

    }

    apply plugin: 'java'

    apply plugin: 'eclipse'

    apply plugin: 'org.springframework.boot'

    apply plugin: 'io.spring.dependency-management'

    apply plugin: 'docker'

    group = 'tanwb'

    archivesBaseName = 'config-server'

    version = 'latest'

    sourceCompatibility = 1.8

    targetCompatibility = 1.8

    repositories {

        mavenCentral()

        maven { url "https://repo.spring.io/milestone" }

        maven { url "https://nexus.nroad.com.cn/repository/maven-snapshots/" }

    }

    ext {

        springCloudVersion = 'Finchley.RELEASE'

    }

    dependencies {

        compile('org.springframework.boot:spring-boot-starter')

        testCompile('org.springframework.boot:spring-boot-starter-test')

        compile('org.springframework.cloud:spring-cloud-config-server')

    }

    dependencyManagement {

        imports {

            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"

        }

    }

    // copy the DockerFile into the build/libs folder

    build.doLast() {

        copy {

            from 'src/main/docker'

            into 'build/libs'

        }

    }

    task buildDocker(type: Docker, dependsOn: build) {

        push = false

        applicationName = archivesBaseName

        dockerfile = file('src/main/docker/Dockerfile')

        doFirst {

            copy {

                from jar

                into stageDir

            }

        }

    }


    在src/main下增加docker目录Dockerfile文件:

    # 运行环境

    FROM frolvlad/alpine-oraclejdk8:slim

    # 时区

    RUN apk add --no-cache bash coreutils tzdata && echo "Asia/Shanghai" > /etc/timezone

    # 挂载点

    VOLUME /tmp

    # 复制到容器并改名

    ADD config-server-latest.jar app.jar

    # 端口映射

    EXPOSE 8001

    # 修改app.jar的访问时间和修改时间为当前时间。

    RUN bash -c 'touch /app.jar'

    #指定容器启动程序及参数

    ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

    命令行打包/打标签/推送阿里云(使用的docker为阿里云镜像服务):

    ./gradlew buildDocker -x test

    docker tag 3cbaf8531b5a registry.cn-hangzhou.aliyuncs.com/tanwb/config-server

    docker push registry.cn-hangzhou.aliyuncs.com/tanwb/config-server

    推送之前请先登陆一下Docker:

    docker login --username=tanwb registry.cn-hangzhou.aliyuncs.com

    附服务器的docker-compose.yml配置:

    config-server:

        container_name: config-server

        image: registry.cn-hangzhou.aliyuncs.com/tanwb/config-server

        ports:

          - 8001:8001

        volumes:

          - /data/docker2/config/native:/native

        entrypoint: java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

        environment:

          - "TZ=Asia/Shanghai"

    以下为jib打包方式:

    修改build.gradle文件(使用的docker为阿里云镜像服务):

    buildscript {

        ext {

            springBootVersion ='2.0.3.RELEASE'

        }

        repositories {

            mavenCentral()

            maven { url"https://plugins.gradle.org/m2/" }

        }

        dependencies {

            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

        }

    }

    plugins {

        id 'com.google.cloud.tools.jib' version'0.9.7'

        id 'com.gorylenko.gradle-git-properties' version'1.5.1'

    }

    applyplugin:'java'

    applyplugin:'eclipse'

    applyplugin:'org.springframework.boot'

    applyplugin:'io.spring.dependency-management'

    group ='tanwb'

    archivesBaseName ='config-server'

    version ='latest'

    sourceCompatibility =1.8

    targetCompatibility =1.8

    repositories {

        mavenCentral()

        maven { url"https://repo.spring.io/milestone" }

        maven { url"https://nexus.nroad.com.cn/repository/maven-snapshots/" }

    }

    ext {

        springCloudVersion ='Finchley.RELEASE'

    }

    dependencies {

        compile('org.springframework.boot:spring-boot-starter')

        testCompile('org.springframework.boot:spring-boot-starter-test')

        compile('org.springframework.cloud:spring-cloud-config-server')

    }

    dependencyManagement {

        imports {

            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"

        }

    }

    def getGitHash = { ->

        def stdout =new ByteArrayOutputStream()

        exec {

            commandLine 'git','rev-parse','--short','HEAD'

            standardOutput = stdout

        }

        return stdout.toString().trim()

    }

    jib {

        from {

            image ="registry.cn-hangzhou.aliyuncs.com/tanwb/openjdk-font:8-jdk-alpine"

        }

        to {

            image ="registry.cn-hangzhou.aliyuncs.com/tanwb/config-server:rc1-${getGitHash}"

        }

        container {

            mainClass ='com.config.server.Application'

        }

    }

    命令行使用:

    ./gradlew jib

    执行后提示

    Built and pushed image as registry.cn-hangzhou.aliyuncs.com/tanwb/config-server:rc1-b70fb53

    BUILD SUCCESSFUL in 6s

    4 actionable tasks: 2 executed, 2 up-to-date

    附服务器的docker-compose.yml配置:

    config-server:

        container_name: config-server

        image: registry.cn-hangzhou.aliyuncs.com/tanwb/config-server:rc1-b70fb53

        ports:

          - 8001:8001

        volumes:

          - /data/docker2/config/native:/native

        entrypoint: java -cp /app/libs/*:/app/resources/:/app/classes/ com.config.server.Application

        environment:

          - "TZ=Asia/Shanghai"

    相关文章

      网友评论

          本文标题:gradle docker 项目构建

          本文链接:https://www.haomeiwen.com/subject/qjywgftx.html