Skip to content

Instantly share code, notes, and snippets.

@oNddleo
Created March 7, 2025 08:45
Show Gist options
  • Save oNddleo/c8d6d7b03eb216d6a0467f4569ebdf65 to your computer and use it in GitHub Desktop.
Save oNddleo/c8d6d7b03eb216d6a0467f4569ebdf65 to your computer and use it in GitHub Desktop.
Docker Maven jdk23

Here's a Dockerfile that uses Maven 4 and JDK 23:

FROM eclipse-temurin:23-jdk-jammy

# Set Maven version
ENV MAVEN_VERSION 4.0.0
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "/root/.m2"

# Install Maven
RUN apt-get update && \
    apt-get install -y curl && \
    mkdir -p /usr/share/maven /usr/share/maven/ref && \
    curl -fsSL https://dlcdn.apache.org/maven/maven-4/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
    | tar -xzC /usr/share/maven --strip-components=1 && \
    ln -s /usr/share/maven/bin/mvn /usr/bin/mvn && \
    # Clean up
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Copy application files (if needed)
# COPY . .

# Default command
CMD ["mvn", "--version"]

This Dockerfile:

  1. Uses Eclipse Temurin's JDK 23 as the base image
  2. Installs Maven 4.0.0
  3. Sets up necessary environment variables
  4. Creates a working directory for your application

You can build this image with:

docker build -t maven4-jdk23 .

And run it with:

docker run -it maven4-jdk23

Would you like me to explain any part of this Dockerfile in more detail?​​​​​​​​​​​​​​​​

@oNddleo
Copy link
Author

oNddleo commented Mar 7, 2025

Here's an updated Dockerfile that builds a Maven application, copies the Java app, and runs it:

FROM eclipse-temurin:23

# Set Maven version
ENV MAVEN_VERSION 4.0.0
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "/root/.m2"

# Install Maven
RUN apt-get update && \
    apt-get install -y curl && \
    mkdir -p /usr/share/maven /usr/share/maven/ref && \
    curl -fsSL https://dlcdn.apache.org/maven/maven-4/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
    | tar -xzC /usr/share/maven --strip-components=1 && \
    ln -s /usr/share/maven/bin/mvn /usr/bin/mvn && \
    # Clean up
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Copy Maven project files
COPY pom.xml .
COPY src ./src

# Build the application
RUN mvn clean package

# Define the JAR file (adjust filename if needed)
ENV JAR_FILE target/*.jar

# Copy the JAR to app.jar
RUN cp ${JAR_FILE} app.jar

# Run the application
ENTRYPOINT ["java", "-jar", "app.jar"]

This Dockerfile:

  1. Uses Eclipse Temurin JDK 23
  2. Installs Maven 4.0.0
  3. Copies your Maven project files (pom.xml and src directory)
  4. Builds the project with mvn clean package
  5. Copies the resulting JAR file to app.jar
  6. Sets the entrypoint to run the Java application

To use this Dockerfile:

  1. Place it in the root directory of your Maven project
  2. Make sure your pom.xml is configured to build an executable JAR
  3. Build the Docker image with docker build -t my-java-app .
  4. Run the container with docker run -it my-java-app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment