Java 和 Docker 新手,尝试使用以下方法对 this(我的第一个 Spring Boot 应用程序)进行 dockerize:
Dockerfile
FROM maven:3.8.4-jdk-11 AS build
COPY src /app/src
COPY pom.xml /app
WORKDIR /app
RUN mvn clean install -U
FROM openjdk:8-jre-alpine
COPY --from=build /app/target/mal-randomizer-0.0.1-SNAPSHOT.jar /app/app.jar
WORKDIR /app
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
昨晚多次出现此错误:
20.09 [INFO] Compiling 8 source files with javac [debug release 21] to target/classes
20.18 [INFO] ------------------------------------------------------------------------
20.18 [INFO] BUILD FAILURE
20.18 [INFO] ------------------------------------------------------------------------
20.18 [INFO] Total time: 19.160 s
20.18 [INFO] Finished at: 2024-04-22T14:49:00Z
20.18 [INFO] ------------------------------------------------------------------------
20.18 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project mal-randomizer: Fatal error compiling: error: release version 21 not supported -> [Help 1]
20.18 [ERROR]
20.18 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
20.18 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
20.18 [ERROR]
20.18 [ERROR] For more information about the errors and possible solutions, please read the following articles:
20.18 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
我尝试了多种解决方案,例如:
JAVA_HOME
环境pom.xml
Java 版本、maven 编译器目标和源**如果我将 Java 版本降级太多(低于 Java 16),我的代码将无法运行,因为记录已在 16 中引入。
我相信解决方案在于
pom.xml
仍然:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.victorfernandesneto</groupId>
<artifactId>mal-randomizer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mal-randomizer</name>
<description>MyAnimeList's Unofficial List Randomizer</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
有两件事并不真正匹配:dockerfile 中的 maven:3.8.4-jdk-11 和 pom.xml 中的
错误消息证明了这一点
error: release version 21 not supported -> [Help 1]
。
此外,您用来运行构建的应用程序的映像显然使用更低的 Java 版本:
openjdk:8-jre-alpine
。pom.xml 中的 Java 版本必须得到构建映像和稍后运行应用程序的映像的支持,因此请使用
maven:3.8.4-jdk-21
(或不同的 Maven 版本)进行构建,并使用 openjdk:21-jre-alpine
进行运行.