首页 > 解决方案 > 使用 devtools 的 Spring Boot docker autoreload

问题描述

我正在使用 Spring boot,并且正在尝试设置一个更新应用程序的 docker 环境,当我在自己的计算机上进行更改时。

我发现我需要使用 Spring Boot 开发工具,并且我已经设置了它。但是即使我点击保存并且可以在控制台中看到它更新,应用程序也不会更新代码。

这是我的 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"]

这就是我在我的POM.xml 中的内容,并且似乎正在工作

弹簧引导开发工具

如您所见,当我点击保存时,这是控制台中的输出。

在此处输入图像描述

我已经检查了容器内部,文件是否已更新,并且确实如此。所以我的问题是 - 为什么我的应用程序没有用新代码更新,即使它说它已经改变并且文件已经改变?我的码头工人有什么问题吗?

标签: javaspringspring-bootdocker

解决方案


我发现这篇文章在设置 Docker 环境时很有帮助。

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

运行.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

码头工人-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

推荐阅读