Home > OS >  Spring boot docker autoreload using devtools
Spring boot docker autoreload using devtools

Time:11-09

I am working with Spring boot and I am trying to setup an docker environment that updates the application, when I make changes on my own computer.

I have found the Spring boot dev tools is what I need to use, and I have setup this already. But the application never updates the code even when I hit save and can see in the console that it updates.

This is my Dockerfile:

#### Stage 1: Build the application
FROM openjdk:8-jdk-alpine as build

# Set the current working directory inside the image
WORKDIR /app

# Copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn

# Copy the pom.xml file
COPY pom.xml .

# Build all the dependencies in preparation to go offline. 
# This is a separate step so the dependencies will be cached unless 
# the pom.xml file has changed.
RUN ./mvnw dependency:go-offline -B

# Copy the project source
COPY src src

# Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

#### Stage 2: A minimal docker image with command to run the app 
FROM openjdk:8-jre-alpine

ARG DEPENDENCY=/app/target/dependency

# Copy project dependencies from the build stage
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app

ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.polls.PollsApplication"]

This is what I have In my POM.xml and seems to be working

spring-boot-devtools

And as you can see, when I hit save, this is the output in the console.

enter image description here

I have checked inside the container, that the files are updated, and they are. So my question is - Why is my application not updating with the new code, even tho it says it has changed and the files IS changed? Is it something with my docker?

CodePudding user response:

This ended up being the solution I was looking for:

Dockerfile

FROM eclipse-temurin:11
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y inotify-tools dos2unix
ENV HOME=/app
RUN mkdir -p $HOME
WORKDIR $HOME

run.sh

#!/bin/bash
dos2unix mvnw
./mvnw spring-boot:run -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" &
while true; do
  inotifywait -e modify,create,delete,move -r ./src/ && ./mvnw compile
done

docker-compose.yml

version: '3.3'
services:
  api:
    build:
      context: ./
      dockerfile: Dockerfile
    volumes:
      - ./:/app
      - ./.m2:/root/.m2
    working_dir: /app
    command: sh run.sh
    ports:
      - 8080:8080
      - 35729:35729
      - 5005:5005
  • Related