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
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
Init project
- Create a new project with Intellij IDEA
- Fill in the information for Spring Artifcat, Gradle, Kotlin, Java: 8
- Select
Spring Web
dependencies
- Select the directory to save the project
- Wait for it to first build dependencies in
build.gradle.kts
- Create a
HomeController
to test whether the project has run ok yet
Build Dockerfile
In this section, we will use multi-stage to build Dockerfile
Docker image ubuntu:18.04
This section will be the temporary environment to build entr
in it should
- package
build-essential curl
is the basicbuild-essential curl
tool needed. curl http://eradman.com/entrproject/code/entr-4.7.tar.gz | tar -xz -C /tmp/
download source and then extract to/tmp
directory- Then go to the directory above to run the command build
entr
:cd /tmp/entr-4.7 && ./configure && make test && make install
after it is done in the/usr/local/bin/entr
Docker image gradle:6.7.1-jdk8
This section plays a role in the project build and listens for file changes
COPY --from=entr_builder /usr/local/bin/entr /usr/local/bin
copy 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
1 2 3 4 5 6 | 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/watchfile
plays the role of listening for the file change event and calling back to theprecompile
above.
Build docker-compose.yml
This section describes the services that need to be run in docker
kt_service
is a service containing code that is assigned thework
volume of the container to the project directory.command: /bin/bash -c 'watchfile'
is the command when usingdocker-compose up
will callwatchfile
written in Dockerfile to listen.
For the first time, need to use the
docker-compose up --build
command to build images, containers, and volumne from Dockerfile.
After running docker-compose up --build
will have the following log:
Certificate that has run successfully.
HomeController test
We’ll try going to the http://localhost:8080
url which will give the output
Next, modify the code from Alive
to Die
and observe
- In the terminal log there is a line
Stop server ...
andStart server ...
ofprecompile
means it is rebuild and start again with> Task :bootRun
- In the browser, reload the page and it will have output
Conclude
Following the steps described, we do not need to manually type commands such as:
docker-compose down
gradle --stop && rm -r /home/gradle/.gradle/daemon && gradle bootRun
docker-compose up
To make receive changes it has automatically received.
Thank you for reading my article. (bow)