While developing a Spring boot application using Kotlin language with docker, it must go through the new build process to start the server, so when the developer will continuously fix the code, it will automatically receive new code. So in this article will introduce a way to take advantage of an entr tool to listen when the file changes will trigger a rebuild and restart the server. To simulate practically building a RESTful application using Spring boot, Kotlin with Gradle manages dependencies in a Docker environment.
Spring boot helps to create stand-alone based on Spring application that only needs to run with a simple configuration which has the following features:
- Create Spring stand-alone application
- Run Embed Tomcat, Jetty or Undertow directly
- Provide ‘starter’ dependencies to simplify configuration
- Automatically configure Spring and 3rd library at any time
- Provides pre-production features like metrics, health checks and external configuration
- Absolutely no code generation and no XML configuration required
- Create a new project with Intellij IDEA
- Fill in the information for Spring Artifcat, Gradle, Kotlin, Java: 8
- Select the directory to save the project
- Wait for it to first build dependencies in
- Create a
HomeControllerto test whether the project has run ok yet
In this section, we will use multi-stage to build Dockerfile
This section will be the temporary environment to build
entr in it should
build-essential curlis the basic
build-essential curltool needed.
curl http://eradman.com/entrproject/code/entr-4.7.tar.gz | tar -xz -C /tmp/download source and then extract to
- Then go to the directory above to run the command build
cd /tmp/entr-4.7 && ./configure && make test && make installafter it is done in the
This section plays a role in the project build and listens for file changes
COPY --from=entr_builder /usr/local/bin/entr /usr/local/bincopy from previous stage to gradle image
- The code below is responsible for stopping the server, deleting the stopped gradle daemons and finally building & restarting the server
RUN echo $'gradle --stop n
rm -r /home/gradle/.gradle/daemon n
echo "Stop server ..." n
echo "Start server ..." n
gradle bootRun' > /usr/local/bin/precompile
RUN echo 'find . -type f ( -name "*kt" ) | entr -r precompile' > /usr/local/bin/watchfileplays the role of listening for the file change event and calling back to the
This section describes the services that need to be run in docker
kt_serviceis a service containing code that is assigned the
workvolume of the container to the project directory.
command: /bin/bash -c 'watchfile'is the command when using
docker-compose upwill call
watchfilewritten in Dockerfile to listen.
For the first time, need to use the
docker-compose up --buildcommand to build images, containers, and volumne from Dockerfile.
docker-compose up --build will have the following log:
Certificate that has run successfully.
We’ll try going to the
http://localhost:8080 url which will give the output
Next, modify the code from
Die and observe
- In the terminal log there is a line
Stop server ...and
Start server ...of
precompilemeans it is rebuild and start again with
> Task :bootRun
- In the browser, reload the page and it will have output
Following the steps described, we do not need to manually type commands such as:
gradle --stop && rm -r /home/gradle/.gradle/daemon && gradle bootRun
To make receive changes it has automatically received.
Thank you for reading my article. (bow)