New to Java and Docker, trying to dockerize this (my first Spring Boot application) with:
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"]
Got this error multiple times last night:
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
I tried multiple solutions such as:
JAVA_HOME
envpom.xml
Java version, maven-compiler target and source**If I downgrade the Java version too much (below Java 16), my code won't run since records have been introduced in 16.
I believe the solution lies in the pom.xml
still:
<?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>
Two things are not really matching: maven:3.8.4-jdk-11
in your dockerfile and <java.version>21</java.version>
in your pom.xml. That means you are trying to build the application with Java 21, but you are using a maven image that supports Java up to version 11, which will not work.
That's proof by the error message
error: release version 21 not supported
… (by an image with JDK 11).
In addition, the image you are using to run the built application obviously uses an even lower Java version: openjdk:8-jre-alpine
.
That will not work either… You're going get a similar error message as soon as the build is done with the correct Java version.
The Java version from the pom.xml must be supported by the build image and by the image that later runs the application, so use a maven:3.8.4-jdk-21
(or a different maven version) for the build and an openjdk:21-jre-alpine
for the run.