How to configure Docker to build and run a jar file using maven?


Hello, so I have been stuck with this issue for over a day. My goal is to create a dockerfile that enables maven to build a jar file. The reason I would like to this is to allow for CI/CD. I want to create a workflow that allows my master github branch to create a docker image and then push it to Docker hub.

Previously I had the standard file that would create an image by adding the built jar file and then running it. The issue with this is that my github repository doesn’t have the target folders, and It shouldn’t have it to my knowledge. Because of this setup, I have to build the image and push it to docker hub manually. This is something I want to turn into a workflow.

What I’ve Been Using:

FROM adoptopenjdk/openjdk11:alpine-jre
COPY ../target/ur-codebin-1.0-SNAPSHOT.jar app.jar
CMD java -jar /app/app.jar

The Errors/Problems:

This issue I am faced now is how to get maven to build the project properly. Every single time I try building the following updated dockerfile, I am left with an error that indicates that node has not been installed. What confuses me is that, to my knowledge the vaadin dependencies are installing node when I build the project. So It doesn’t make sense why I’m getting this error.

Updated Dockerfile

FROM maven:3.5.2-jdk-8-alpine
COPY pom.xml .
COPY src ./src/
COPY frontend ./frontend/
RUN mvn clean install -Pproduction  #I get the error at this step

CMD java -jar ./target/ur-codebin-1.0-SNAPSHOT.jar

Snippet of The Error:

#10 65.74 [INFO] Couldn't find node. Installing Node and NPM to /root/.vaadin.
#10 65.75 [INFO] ------------------------------------------------------------------------
#10 65.75 [INFO] ------------------------------------------------------------------------
#10 65.75 [INFO] Total time: 01:03 min
#10 65.75 [INFO] Finished at: 2020-10-20T18:26:14Z
#10 66.00 [INFO] Final Memory: 45M/352M
#10 66.00 [INFO] ------------------------------------------------------------------------
#10 66.00 [ERROR] Failed to execute goal com.vaadin:vaadin-maven-plugin:14.3.7:prepare-frontend (default) on project ur-codebin: Failed to install Node: Unable to detect version of Node. Using command /root/.vaadin/node/node --version: Cannot run program "/root/.vaadin/node/node": error=2, No such file or directory -> [Help 1]
#10 66.00 [ERROR]
#10 66.00 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
#10 66.00 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
#10 66.00 [ERROR]
#10 66.00 [ERROR] For more information about the errors and possible solutions, please read the following articles:
#10 66.00 [ERROR] [Help 1]
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c mvn clean install -Pproduction]: runc did not terminate sucessfully

What confuses me about this is that if I go up the stack trace, I see the following stack trace which clearly states that "node installation was successful" at /root/.vaadin/node/node

#10 59.83 [INFO] Couldn't find node. Installing Node and NPM to /root/.vaadin.
#10 59.84 [INFO] Installing node version v12.16.0
#10 59.84 [INFO] Downloading to /root/.vaadin/node-v12.16.0-linux-x64.tar.gz
#10 59.84 [INFO] No proxies configured
#10 59.84 [INFO] No proxy was configured, downloading directly
#10 62.81 [INFO] Unpacking /root/.vaadin/node-v12.16.0-linux-x64.tar.gz into /root/.vaadin/node/tmp
#10 64.68 [INFO] Copying node binary from /root/.vaadin/node/tmp/node-v12.16.0-linux-x64/bin/node to /root/.vaadin/node/node
#10 64.87 [INFO] Extracting NPM
#10 65.70 [INFO] Local node installation successful.
#10 65.70 [WARNING] Error checking if node is new enough
#10 65.70 com.vaadin.flow.server.frontend.FrontendUtils$UnknownVersionException: Unable to detect version of node. Using command /root/.vaadin/node/node --version

Does anyone have any ideas on how I could approach this problem?
Note: If you need more of the stack trace, I’ll gladly send some sort of file with the full trace.

Source: Docker Questions