Gradle使用Docker部署 Spring Boot项目记录

一、Docker来部署Spring Boot的应用

修改build.gradle文件中添加 dockerBuilder方法;

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath 'gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.19.2'
    }
}

apply plugin: 'com.palantir.docker'
group = 'springio'
bootJar {
    baseName = 'gs-spring-boot-docker'
    version = '0.1.0'
}

dockerbuild {
    dependsOn build
    name "${project.group}/${bootJar.baseName}"
    files bootJar.archivePath
    buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}

在项目中创建/docker/Dockerfile文件;

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

二、打包镜像

gradle clean dockerbuild       #打包镜像
subprojects {

    dependencies {
        implementation project(":hsh-medical-common")
    }
    apply plugin: 'org.springframework.boot'

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

        tag = "${applicationName}"
        tagVersion = "latest"          #容器标签

        def dockerFolder = "${dockerFolderBase}" + tag
        //指定dockerfile所在的目录为编译目录,会自动拷贝dockerfile文件到stageDir目录
        stageDir = file("$dockerFolder")
        dockerfile = file("/docker/Dockerfile") #dockerfile文件所在项目的位置;

        doFirst {
            //复制jar包
            copy {
                from jar
                into "$dockerFolder"
            }
            copy {
                from "$dependenceFolder" + "/java_start.sh"
                into "$dockerFolder"
            }
        }
    }

}

你可能感兴趣的:(docker,gradle,docker,java)