Set auto build system in Docker Spring boot container
DockerHi, I’m Lovefield.
I wrote about how to use docker-compose to raise Spring boot the other day. Implementing and managing a microservice environment with Docker is a good thing, but for apps that require build, there is a pretty cumbersome process.You need to update the resources, build, and then restart the container. In the case of the build tool of Spring boot, if it is not cached, it will start downloading all modules. Caching is not possible due to the nature of the Docker that deletes and reconfigures the container.
So, You want to configure an environment that automatically builds and restarts spring boot containers using Docker volumes and Git hook file.
First, write the docker-compose.yml
file.
…
spring:
container_name: appname
image: openjdk:11.0.7-jdk-slim
volumes:
- ./appname:/spring
- ~/.gradle:/root/.gradle:rw # cache folder
working_dir: /spring
command: java -jar ./build/libs/appname-0.0.1-SNAPSHOT.jar
…
Using image openjdk:11.0.7-jdk-slim
. What's important here is that you have to use “jdk” to have a tool for building. If you use “jre” for execution, the build will not run, so please be careful. Use the slim version to reduce image capacity.
In Mac ~/.gradle
is the space where the cache used by Gradle when building is stored. And the container's /root/.gradle
is where Gradle finds the cache when executing a command inside the container. It synchronizes caching data locally with the container.
The container is now synchronized with the Gradle cache with the local environment. When using pull and checkout with Git, I plan to build in the container and restart the container so that the change can be applied.
Create files post-checkout
and post-merge
of Git hook. The default Git hook path is .git/hooks
. Write the following commands in each file:
docker exec appname ./gradlew build -x test && docker-compose restart spring
All settings are now complete. If you use pull or checkout in the spring repo, you can see the build and restart.