akuma8
akuma8

Reputation: 4691

Unable to run './mvnw clean install' when building docker image based on "openjdk:8-jdk-alpine" for Spring Boot app

I would like to build a Spring Boot app using the maven wrapper provided by spring.io.starter inside a docker container. My Dockerfile is:

FROM openjdk:8-jdk-alpine
# install bash --> commented because I just need /bin/sh which is already provided by the base image
#RUN apk add --no-cache bash
ENV APP_DIR /app
ENV APP app.jar
WORKDIR ${APP_DIR}
COPY . ${APP_DIR}
# ===> HERE THE PROBLEM
RUN /bin/sh -c "./mvnw clean install"
ENTRYPOINT ["java","-jar","chicowa-app.jar"]
EXPOSE 8080

I have this error:

/bin/sh: ./mvnw: not found

After making some researches I still don't find a solution. My docker version Docker version 18.06.1-ce, build e68fc7a on Windows10 pro

Thanks a lot for your help.

EDIT

A solution would be to install maven directly with a RUN apk add --no-cache maven but I would like to minimize as possible the image' size.

Upvotes: 3

Views: 13065

Answers (9)

user10942209
user10942209

Reputation: 1

I had the same problem, I just create another Spring project with Initializer and copy / paste mvnw and mvnw.cmd in the "old" project: after that it was found by Docker (version bump from 3.8.6 to 3.8.7 don't know if it was the problem).

Just edit .mvn/maven-wrapper.prop to be in part the with new version after that also.

Upvotes: -1

Murat Yıldız
Murat Yıldız

Reputation: 12050

The problem can also be fixed without bothered with line endings or path variables even on Windows10.

Just include Maven to your Dockerfile and run the maven commands via this:

Here is an example Dockerfile:

FROM maven:3.8.1-openjdk-17-slim AS builder   # <-- Include Maven
WORKDIR /app
COPY pom.xml ./
COPY src ./src

RUN mvn clean install

# Second stage: Minimal runtime environment
FROM eclipse-temurin:17-jre-jammy
WORKDIR /app

# copy jar from the first stage
COPY --from=builder /app/target/*.jar /app/app.jar

EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

After using this approach, I solved this problem. Hope this helps...

Upvotes: -1

JABResearcher
JABResearcher

Reputation: 9

This is working solution as I don't find anywhere this solution:

mvnw -version error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

Root Cause: maven public certificate is missing in java trust store

Solutions:

  1. download - repo.maven.apache.org leaf certificate from https://repo.maven.apache.org and save it as repo_maven.cer file in base64 format
  2. Install the repo_maven.cer into to cacerts (truststore) keytool -import -keystore "...\openjdk-8u41\jre\lib\security\cacerts" -file "repo_maven.cer" -alias maven

Output: mvnw -version Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)

Upvotes: -1

Muhammad Ehtisham
Muhammad Ehtisham

Reputation: 56

use

RUN chmod +x mvnw 

before using

./mvnw clean install 

it'll make your mvnw file executable

Upvotes: 1

Matheus Rangel
Matheus Rangel

Reputation: 1

If you are using windows git may change the line ending of the mvnw file to CRLF. If you open the file with any text editor and save it to LF it will probably work.

Upvotes: 0

Minh Toan
Minh Toan

Reputation: 111

I have the same issue when build Dockerfile in Window 10. This because mvnw file contains CRLF (\r\n) at the end of line. I open it with Notepad++ and use Find \r\n and Replace with \n (with Extended (\n, \r, ...) checked). You can use dos2unix tool to convert as @Juanan said above.

After that, the build run with no problem.

Upvotes: 6

Juanan
Juanan

Reputation: 61

We were experimenting this issue only on Windows 10 and were able to solve this issue on just applying dos2unix

dos2unix mvnw

Upvotes: 6

cammando
cammando

Reputation: 616

if you have already maven configured then you may run the following:-

mvn clean install

It will build the docker image seamlessly.

Upvotes: 1

Don Simon
Don Simon

Reputation: 154

Unless you are running the script in the exact location that the file mvnw is located, it won’t work.

If your PATH is set correct, and Maven was installed in a suitable location, simply removing the “./“ in front of the command will suffice.

RUN /bin/sh -c “mvnw clean install”

If mvnw is NOT in your PATH, you can specify the full path in your script (but recognize that Maven is likely to call other things that would want the same PATH changes).

Upvotes: 1

Related Questions