Sunday, 1 October 2017

Dockerizing Spring Boot microservice application

Assuming you have a Spring Boot app built, tested and ready to be packaged in to a container for deployment.

I am using SpringBoot with Gradle, precisely spring-boot-gradle-plugin:1.5.7.RELEASE.

Well you can create separate app and then build Docker image manually, however when using Gradle there are good plug-ins available and one I am using is Transmode/gradle-docker plug-in https://github.com/Transmode/gradle-docker

Gradle config for Docker using Transmode plug-in


group 'com.mp'
version '1.2.1-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'docker'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

buildscript {

    ext {
        springBootVersion = '1.5.7.RELEASE'
    }

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE")
        classpath "se.transmode.gradle:gradle-docker:1.2" // <- Here
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    // tag::jetty[]
    compile("org.springframework.boot:spring-boot-starter-web")
    compile 'com.google.code.gson:gson:2.2.4'
}

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.bmuschko:gradle-docker-plugin:2.2'
    }
}

jar {
    baseName = 'internalapis'
    version = '0.1.0'
}

task buildDocker(type: Docker) {
    baseImage = 'develar/java:latest'
    push = project.hasProperty('push')
    tag = 'manmohanpanda/newway-internalapis'
    addFile {
        from jar
        rename {'newway-internalapis.jar'}
    }
    entryPoint(['java', '-Djava.security.egd=file:/dev/./urandom', '-jar', '/newway-internalapis.jar'])
    exposePort(9000)
}

buildDocker.dependsOn(build)
Then simply build your app, this will create Dockerfile under /build with required instruction.

Push Docker image to Docker Hub


./gradlew buildDocker -Ppush=true


Then you can run the image locally or download to Kubernets to deploy.

1 comment:

Microservices architecture with Kubernets

Kubernetes is an  open-source platform for automating deployment, scaling, and operations of application containers  across clusters of hos...