Spring Boot: live reload with Docker

2019-04-01T20:00Z


Summary


Overview

When we develop web applications with Spring Boot, we have to restart the server to pick up all our changes. This kills productivity. Spring Boot Developers Tools provides solutions to automatically pick up changes without a complete server restart.

The Problem With Server Restarts

When we develop our applications (web or RESTful API), we want to be able to test our changes quickly. Typically, in the Java world, we need to restart the server to pick up changes.

That's where Spring Boot Developer Tools comes into the picture.

Adding Spring Boot Developer Tools to your project is very simple. First, add this dependency to your Spring Boot Project gradle.build:

compile 'org.springframework.boot:spring-boot-devtools'

Dockerizer

Create a file in the root of the project, called 'Dockerfile', I've used this Dockerfile's template:

FROM openjdk:11-jre
 
CMD ["gradle"]
 
ENV GRADLE_HOME /opt/gradle
ENV GRADLE_VERSION 5.3.1
 
ARG GRADLE_DOWNLOAD_SHA256=1c59a17a054e9c82f0dd881871c9646e943ec4c71dd52ebc6137d17f82337436
RUN set -o errexit -o nounset \
 && echo "Downloading Gradle" \
 && wget --no-verbose --output-document=gradle.zip "https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip" \
 \
 && echo "Checking download hash" \
 && echo "${GRADLE_DOWNLOAD_SHA256} *gradle.zip" | sha256sum --check - \
    \
    && echo "Installing Gradle" \
    && unzip gradle.zip \
    && rm gradle.zip \
    && mv "gradle-${GRADLE_VERSION}" "${GRADLE_HOME}/" \
    && ln --symbolic "${GRADLE_HOME}/bin/gradle" /usr/bin/gradle \
 \
 && echo "Adding gradle user and group" \
 && groupadd --system --gid 1000 gradle \
 && useradd --system --gid gradle --uid 1000 --shell /bin/bash --create-home gradle \
 && mkdir /home/gradle/.gradle \
 && chown --recursive gradle:gradle /home/gradle \
 \
 && echo "Symlinking root Gradle cache to gradle Gradle cache" \
 && ln -s /home/gradle/.gradle /root/.gradle
 
USER gradle
VOLUME "/home/gradle/.gradle"
WORKDIR /home/gradle
 
RUN set -o errexit -o nounset \
 && echo "Testing Gradle installation" \
 && gradle --version

Info: this is the official Gradle Docker Hub

Create a file in the root of the project, called 'docker-compose.yml':

version: '3.3'
services:
spring-boot-test:
build:
context: ./
dockerfile: Dockerfile
 volumes: - ./:/app
working_dir: /app
command: sh run.sh
ports: - 8080:8080

And create another file in the root of the project, called 'run.sh':

#!/bin/bash
gradle --stop
gradle build --continuous --quiet &
gradle bootRun

Build and Run!

docker-compose build
docker-compose up