简体   繁体   中英

Spring Boot DevTools being used inside docker container even after exclusion in gradle build

So we are using Spring boot to deliver our application. We use the Jib plugin to monitor create docker images and run them. We use gradle to build the project and there dev tools is identified as a developmentOnly dependency. Just as mentioned in the spring docs at https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools .

However when it runs in the container in prod I still see it getting restarted now and then. My question is does the gradle configuration not really exlude it from packaging. Do i need to explicitly set the -Dspring.devtools.restart.enabled=false parameter?

Solution:

So turns out it was the gradle jib plugin playing games. While the spring documentation is spot on about how to go about removing the dependency from gradle spring boot project. The technique of specifying a developmentOnly only helps in telling gradle to ignore the dev tools. The jib gradle plugin has a mind of its own.

It includes all jars when building a docker image and there is no way to exclude any jar. The only reasonable way is to customize the gradle jib plugin in build.gradle to write this,

 jib {
    from {
        image 'gcr.io/distroless/java:11'
    }
    container {
        jvmFlags = ['-Xms1G', '-Xmx1G', '-Dspring.devtools.restart.enabled = false']
    }
}

This will make sure that even if the jar is included the container environment has the restart taken care of.

Reference: https://github.com/spring-projects/spring-boot/issues/15382

You can achieve that by setting spring.devtools.restart.enabled=false in your application.properties or your specific profile properties ex. application-cloud.properties . Let me know if this works.

There's really a few problems here:

  1. Springboot has its own custom definitions instead of using what would be the equivalent of profiles. Their approach is best for springboot users, but rather hard to integrate with given all their custom logic.
  2. Jib can't know all the custom implementation of each framework.

I really think what you should be doing is something like this:

dependencies {
  if (System.getProperty("development") == true) {
    // include the springboot devtool dependency
  }
}

When you want to run or build in dev mode, just do

./gradlew whateverTask -Ddevelopment=true

Well, just faced the same problem recently and it seems there's already a very straight-forward way to solve it.

The problem

Jib is actually considering spring-boot-devtools as a runtime dependency and so, adding it to the image. In my case, this is also true for h2 database jar which is used only locally for development. Also, I wouldn't like to deal with any extra custom parameter in my build, neither turn-off features by configuration if I don't really wanna them available at production.

Solution

GoogleContainerTools' team has released a jib-extension to deal with devtools problem directly. There's a Gradle and Maven version and it works flawlessly.

However, to my needs (also exclude h2) I've decided to use jib layer filter extension so I can keep my image as close to bootJar as possible.

Here goes the code snippet in gradle:

// should be at the top of build.gradle
buildscript {
    dependencies {
        classpath('com.google.cloud.tools:jib-layer-filter-extension-gradle:0.1.0')
    }
}
jib {
    // ...
    pluginExtensions {
        pluginExtension {
            implementation = 'com.google.cloud.tools.jib.gradle.extension.layerfilter.JibLayerFilterExtension'
            configuration {
                filters {
                    filter {
                        glob = '**/h2-*.jar'
                    }
                    filter {
                        glob = '**/spring-boot-devtools-*.jar'
                    }
                }
            }
        }
    }
}

Check here the Gradle and Maven version for this extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM