1

I've created a spring starter project with gradle as dependency manager and also including spring-boot-dev-tools. My aim is to create a docker image so that there must be live reload whenever I change a java file inside docker container. This is working well with following configurations.

Dockerfile

FROM gradle:5.6.2-jdk8
WORKDIR /app
COPY . /app
RUN gradle build
EXPOSE 8080
CMD ["sh", "start.sh"]

start.sh

#!/bin/bash
gradle --version
gradle build --continuous &
gradle bootRun

This is working

One of my controller file is as follows.

@RestController
public class MainController {
    @GetMapping("/")
    String test() {
        return "Test Success";
    }
}

Getting "Test Success" in browser when navigating to localhost:8080. When the file /app/.../MainController.java inside docker container is changed to say, return "Updated";, the change is affected automatically.

This is the issue

Even if there is RUN gradle build inside Dockerfile, docker run command with this image seems to be downloading the gradle dependencies again because of the command gradle build --continuous or gradle bootRun.

I want the docker run command fast, like it's downloaded all the dependencies already. Please suggest a workaround. Live reload is needed.

Ramanujan R
  • 1,601
  • 2
  • 23
  • 43
  • Have you tried mapping your ~/.gradle folder from your host machine to /root/.gradle in the container ? – Jonath P Nov 20 '20 at 14:11
  • @JonathP Thanks for your support. I'd resolved the issue, but forgot to post the answer. By mapping gradle dependencies from host machine, a dependency to host will be created there and the Docker image won't be able to run in a host machine without gradle. – Ramanujan R Nov 23 '20 at 12:06

1 Answers1

0

I've used getDeps option inside the Dockerfile and skipDownload in start.sh file. This will download all the dependencies during Docker build and the live reload is now fast.

Dockerfile

FROM gradle:5.6.2-jdk8
WORKDIR /app
COPY . /app

RUN gradle --no-daemon --warning-mode all --console=plain getDeps

EXPOSE 8080
CMD ["sh", "start.sh"]

start.sh file

#!/bin/bash
gradle --version
gradle build --continuous -PskipDownload=true --console=plain &
gradle bootRun -PskipDownload=true --console=plain

build.gradle

// other configs
ext {
    skipDownload = project.hasProperty('skipDownload') ? project.getProperty('skipDownload') as boolean : false
}    
repositories {
    if (skipDownload) {
        flatDir {
            dirs 'libs'
        }
    } else {
        mavenCentral()
    }
}
dependencies {
    if (skipDownload) {
        implementation files('libs/spring-boot-starter-data-jdbc-2.3.7.RELEASE.jar')

    } else {
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jdbc', version: '2.3.7.RELEASE'

    }
}
task getDeps(type: Copy) {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from configurations.compileClasspath into "libs/"
    from configurations.runtimeClasspath into "libs/"
}
Ramanujan R
  • 1,601
  • 2
  • 23
  • 43
  • Where is getDeps? Also, how do you deal with local Gradle dependencies? Do you run a Maven/Gradle repository service on-prem? – ben3000 May 04 '21 at 01:51
  • @ben3000 Please see the Dockerfile `RUN gradle ..... getDeps`. I don't understand the second question. What do you mean by local deps? This is a container and every build process happens inside the container (or intermediate container during docker build). So all we need to do is to create the image from a base gradle image - `FROM gradle:5.6.2-jdk8`. – Ramanujan R May 04 '21 at 03:55
  • 1
    Again I had another problem with this setup and improved it further. Please see this for details - https://stackoverflow.com/questions/59663235/spring-boot-live-reload-inside-a-docker-container-not-working – Ramanujan R May 04 '21 at 03:57
  • Is `getDeps` a gradle task that you've developed, or one provided by gradle? By local dependencies, I mean libraries that I have built that I [manually publish to Maven local](https://docs.gradle.org/current/userguide/publishing_maven.html), is there a way to handle this? – ben3000 May 04 '21 at 04:07
  • 1
    @ben3000 I've updated the Answer with relevant gradle settings. Thanks for pointing this out. – Ramanujan R May 04 '21 at 04:30