Issue encountered when building docker container for Java/ Maven based application

I am currently working on a Robotic automation application based on Java 17, The build tool used in this application is Maven 3.9.2. Iam working on Ubuntu LTS 18.04. I have been successful in building this application in a local development environment. To do so the used command was

mvn clean install -Pdev -DactiveProfile=dev -DconfigFile=config-dev.properties

This command is used to build and install a Maven project with specific profiles and configuration properties. In summary, the command is cleaning the project, compiling and packaging it, and then installing it in the local Maven repository. It’s also activating the “dev” profile and specifying a configuration file named “config-dev.properties” for use during the build.

My Issue is when I attempt to Dockerize this application I am facing a few issues. I attempted to update the Docker file to handle the local development scenario. How can I resolve these issues encountered when attempting to Dockerize the Java 17 ’ Maven-based application? Am I missing something? How can the Maven-based parameterized commands be handled when using docker containers.

Docker File

# Use the official Maven image as a build stage
FROM maven:3.8-openjdk-17 AS builder

# Set the working directory
WORKDIR /app

# Copy the source code to the working directory
COPY . .

# Build the Maven project and create the JAR file
ARG MAVEN_OPTS="-Pdev -DactiveProfile=dev -DconfigFile=config-dev.properties"
RUN mvn clean install $MAVEN_OPTS

# Use the official OpenJDK 17 image as the final image
FROM openjdk:17

# Set the working directory inside the container
WORKDIR /app

# Copy the JAR file from the builder stage
COPY --from=builder /app/target/report-automation.jar /app/report-automation.jar

# Copy the appropriate config file based on Maven profile
COPY src/main/resources/config-dev.properties /app/src/main/resources/config-dev.properties

# Copy the Configuration.xlsm file from the source to the working directory in the container
COPY src/main/resources/Configuration.xlsm /app/src/main/resources/Configuration.xlsm

# Expose the port (if your application listens on a specific port)
# EXPOSE 8080

# Set the entry point for the container (replace with your main class)
ENTRYPOINT ["java", "-jar", "report-automation.jar"]

Docker build command used docker build --build-arg MAVEN_OPTS=“-Pdev -DactiveProfile=dev -DconfigFile=config-dev.properties” -t selenium-app .

Encountered Error

docker build --build-arg MAVEN_OPTS="-Pdev -DactiveProfile=dev -DconfigFile=config-dev.properties" -t selenium-app .
[+] Building 3.7s (10/14)                                                                                                                                                    
 => [internal] load build definition from Dockerfile                                                                                                                    0.0s
 => => transferring dockerfile: 1.19kB                                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/openjdk:17                                                                                                           2.6s
 => [internal] load metadata for docker.io/library/maven:3.8-openjdk-17                                                                                                 2.6s
 => [internal] load build context                                                                                                                                       0.1s
 => => transferring context: 33.52kB                                                                                                                                    0.0s
 => [builder 1/4] FROM docker.io/library/maven:3.8-openjdk-17@sha256:3a9c30b3af6278a8ae0007d3a3bf00fff80ec3ed7ae4eb9bfa1772853101549b                                   0.0s
 => [stage-1 1/5] FROM docker.io/library/openjdk:17@sha256:528707081fdb9562eb819128a9f85ae7fe000e2fbaeaf9f87662e7b3f38cb7d8                                             0.0s
 => CACHED [builder 2/4] WORKDIR /app                                                                                                                                   0.0s
 => [builder 3/4] COPY . .                                                                                                                                              0.6s
 => ERROR [builder 4/4] RUN mvn clean install -Pdev -DactiveProfile=dev -DconfigFile=config-dev.properties                                                              0.4s
------
 > [builder 4/4] RUN mvn clean install -Pdev -DactiveProfile=dev -DconfigFile=config-dev.properties:
#0 0.291 Unrecognized option: -Pdev
#0 0.291 Error: Could not create the Java Virtual Machine.
#0 0.291 Error: A fatal exception has occurred. Program will exit.
------
ERROR: failed to solve: executor failed running [/bin/sh -c mvn clean install $MAVEN_OPTS]: exit code: 1

You have a maven error message. It will be the same in a container and outside containers.

You could try to type the parameters again directly in the RUN instruction instead of using a variable just for testing. When you see “unrecognized option”, it is sometimes caused by an invalid character which is not visible on the screen. Maven 3.8 has that option (I tried) and would work in a container (I was sure but I tried) so all I can imagine is some character issue. Sometimes for example “-” is replaced with something similar when you coopy it from a tutorial.

By the way you are building an image, not a container. You could run a container from a built image.

Thank you for the response. I checked it locally and this command works but in the container, it seems to fail. Checked the special character scenario but I couldn’t verify any such characters.

command used - mvn clean install -Pdev -DactiveProfile=dev -DconfigFile=config-dev.properties

Nevertheless, I refactored the code and removed the dependency for the -Pdev option. Once I did this the existing error was resolved.

New commands used in refactored code

mvn clean install -DactiveProfile=dev -DconfigFile=config-dev.properties

docker build --build-arg MAVEN_OPTS=“-DactiveProfile=dev -DconfigFile=config-dev.properties” -t selenium-app .

Updated the above-mentioned Docker file with this change

# Build the Maven project and create the JAR file
ARG MAVEN_OPTS=“-DactiveProfile=dev -DconfigFile=config-dev.properties”
RUN mvn clean install $MAVEN_OPTS