I’m working on Dockerizing my Maven project and I’m using the following Dockerfile:
# Use an official Maven image as the base image
FROM maven:3.6.0-jdk-11 as builder
# Set the working directory to /app
WORKDIR /app
# Copy the pom.xml file
COPY pom.xml .
# Copy the source code
COPY src/main/java/com/i2i/intern/pixcell/hazelcastoperation /app/src/main/java/com/i2i/intern/pixcell/hazelcastoperation
# Build the project
RUN mvn clean package
# Use an official OpenJDK 11 image as the base image
FROM openjdk:11
# Copy the JAR file from the builder stage
COPY ./target/HazelcastOperation-1.0-SNAPSHOT.jar /app/HazelcastOperation.jar
# Set the default command to run the JAR file
CMD ["java", "-jar", "HazelcastOperation.jar"]
However, I’m encountering an issue when trying to run the container. The container logs always say that the JAR is unable to be accessed or executed.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.i2i.intern.pixcell</groupId>
<artifactId>HazelcastOperation</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<exec.mainClass>com.i2i.intern.pixcell.hazelcastoperation.HazelcastOperation</exec.mainClass>
</properties>
<dependencies>
<!-- Hazelcast Dependency -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>5.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.i2i.intern.pixcell.hazelcastoperation.HazelcastOperation</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I’m not sure if I’m missing something in my Maven configuration or Dockerfile setup. Do I need to load Maven and source codes each time, or is there a better way to handle this? Any guidance on resolving the “JAR unable to be accessed” error would be greatly appreciated!
4