java - 使用 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 中的内容,并且似乎正在工作
如您所见,当我点击保存时,这是控制台中的输出。
我已经检查了容器内部,文件是否已更新,并且确实如此。所以我的问题是 - 为什么我的应用程序没有用新代码更新,即使它说它已经改变并且文件已经改变?我的码头工人有什么问题吗?
解决方案
我发现这篇文章在设置 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
推荐阅读
- python - Django:CreateView 中的 transaction.atomic,form_valid()
- c - 是否有适用于 Mac 的 Eclipse 的 C 调试器?
- javascript - 通过 Ajax PHP 和 JavaScript 在表中插入模态数据
- ruby - 比较日期在不同年份不起作用但在同一年份工作正常
- android - 程序类型已经存在:com.google.android.gms.internal.measurement.zzdz
- javascript - JavaScript / Angular将数组中对象的一个属性复制到另一个对象数组?
- jupyter-notebook - 如何使用 jupyter 的 timeit 获取正确的全局变量?
- eclipse - 无法使用 Tomcat 8.5、Java 8 和 Eclipse Photon 4.8.0 为 JSP 编译类
- r - R lavaan 中的高阶相互作用
- .net - Is this controller untestable?