【问题标题】:Spring boot docker autoreload using devtools使用 devtools 的 Spring Boot docker autoreload
【发布时间】:2021-12-20 05:20:06
【问题描述】:

我正在使用 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不能用作实时重载环境;它不包括任何原始 Java 源文件、它们的 PNG 渲染或 JDK。我建议在这里使用普通的非 Docker Java 开发环境。

标签: java spring spring-boot docker


【解决方案1】:

我发现this article 在设置 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

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

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多