Spring Boot live reload with Docker

Many a times Spring Boot developers use DevTools for development of applications in their local environment. Developers always want to mimic the development environment as close as possible with the production environment. The solution is Docker. But when it comes to dockerizing the application, it becomes a tedious process. Because every time the application is changed, new image has to be created and a container has to be spun up in order for the new changes to reflect. It would really be great if there was some mechanism that would automate this process.

The good news is, there is a way we can achieve it using DevTools. In this article we will discuss that.

When your application is launched using java -jar or it is started using a special class loader then the application is considered to be a production application and Developer tools are automatically disabled in this scenario.

Same is the case when applications are dockerized. So that prevents the dockerized application to live reload. The good news is there is a way to configure our application to live reload the container. Because DevTools and the cloud native build packs are ready to handle such kind of development situations.

To demonstrate this we will start with creation of a Spring Boot application. We will generate the project using spring initializr.

Generate the project and import the project into Intellij.

Let us create a simple endpoint.

We will go ahead and run the application. Once the application is up, we will hit the endpoint from postman. We must receive the response as “Hello YouTube! How are you doing today?”.

Now let us change the string and save the change. Behind the scenes DevTools will reload the application without restarting the JVM. If you now hit the same endpoint then the response should be the modified string.

Starting in spring boot 2.3, spring provides this magnificent functionality that will take the existing application and will build a docker image that can run anywhere.

So to do that we will run the command;

mvnw spring-boot:build-image

Note: You can write Dockerfile and build the image on your own but for the purpose of this demo we are utilizing the image building capability of Spring Boot 2.3.

The above command will compile, unit test and build the image of your application. Once the image is ready, go ahead and run a container using that image. And stop the application that is running in the IDE. Now you should be able to access the same endpoint from postman. And you must get the same response.

Now if you change the string in the helloPXP() method and save the change then you will notice that the container will not reload by itself.

To make it happen, let us start with adding a secret in the application.propertiess file as shown below.

We will also change our POM to let the spring boot know not to exclude the DevTools during build. We will do so by adding this configuration tag.

Let us rebuild the image. While the image is building, we will perform the final step in the run configuration. In the configuration, instead of running our main application, we will run the RemoteSpringApplication.

This Remote Spring application is essentially a file watcher that constantly looks for file change. As and when files are changed the running application is notified that the files have changed.

We will go ahead and run our container again. We will start up the RemoteSpringApplication in the IDE. Let us access the same endpoint using postman. You must get the response. Now change the return string. This time the container should live reload and if you hit the same endpoint once again then you must get the updated string in the response.

To watch the live demo, please watch my YouTube video.

Please let me know in case if there are any further queries or suggestions. Thank you for showing interest and reading through!