Hansen
Hansen

Reputation: 690

Build docker error: "/bin/sh: 1: ./mvnw: not found"

I try to build a Maven project using dockerfile, but I got this error when I used command docker build:

/bin/sh: 1: ./mvnw: not found

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

But I still got an error.

Here is my dockerfile:

# Stage 1 Build the Spring Project into a JAR file
FROM openjdk:8-slim as builder
RUN mkdir src
COPY . /src
WORKDIR /src
RUN chmod 700 mvnw && ./mvnw clean install package  ==> An error on this line

# Stage 2 Run the JAR file from the previous build
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
COPY --from=builder /src/target /build
WORKDIR /build
EXPOSE 80
EXPOSE 5001
ENTRYPOINT ["java", "-jar", "tax-0.0.1-SNAPSHOT.jar"]

Am I missing something?

Upvotes: 19

Views: 27224

Answers (15)

Karyofyllia
Karyofyllia

Reputation: 1

For those who want to build their app with docker-compose on Windows, I the same error :

exec: ".\mvnw": executable file not found in $PATH: unknown

or

/bin/sh: 1: ./mvnw: not found

The solution that worked for me was to change the 1st line of the mvnw file from #!/bin/sh to #!/bin/bash.

Upvotes: 0

natedennis
natedennis

Reputation: 117

install bash in your docker image.

#!/bin/bash

Upvotes: 1

user10942209
user10942209

Reputation: 1

Got the same pb i just create another Spring project with Initialzr and copy / paste mvnw and mvnw.cmd in the "old" projet : after that it was found by Docker (version bump from 3.8.6 to 3.8.7 don't know if it was the pb).

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

Upvotes: 0

Alex Ling
Alex Ling

Reputation: 31

There are two ways I had tested that can solve it:

  1. use dos2unix, converting line endings after copying the mvnw file, like this:

     # syntax=docker/dockerfile:1
    
     FROM eclipse-temurin:17-jdk-jammy
    
     WORKDIR /app
     COPY .mvn/ .mvn
     COPY mvnw pom.xml ./
    
     # Converting the mvnw line endings during build (if you don’t change line endings of the mvnw file)
     RUN apt-get update && apt-get install -y dos2unix
     RUN dos2unix ./mvnw
    
     RUN ./mvnw dependency:resolve
    
     COPY src ./src
    
     CMD ["./mvnw", "spring-boot:run"]
    
  2. use a text editor (like Visual Studio Code), open the mvnw file, and change line endings in the bottom right directly. It’s easier!

Upvotes: 3

Tanushree Paul
Tanushree Paul

Reputation: 41

solution was to convert the line endings to Linux EOL (\n called "LF") from my IDE.

This is correct as stated in the posts by helvete and Uyric.

In Visual Studio Code I just opened the "mvnw" file and selected "LF" from the VSCode bottom-right status bar. Next, the docker build worked fine.

Upvotes: 4

Horse Zhu
Horse Zhu

Reputation: 11

I also found that once we have the CRLF error, the mvnw file may be cached. So while we run the docker build again after the CRLF, it was replaced with CR. We should add --no-cache to make it take effect.

Upvotes: 0

Haider Lasne
Haider Lasne

Reputation: 21

Create a bat file with the below script to convert \r\n to \n:

powershell -NoProfile -command "((Get-Content 'mvnw') -join \"`n\") + \"`n\" | Set-Content -NoNewline 'mvnw1'"

Upvotes: 0

Richie
Richie

Reputation: 586

Absolutely. If the file has a line ending of Windows format it will not be parsed as a Linux file. Therefore, it will result into not found file.

To solve it, install sudo apt-get install dos2unix and run dos2unix mvnw. This converts the file into a Linux line ending type, so it is recognized as a Linux file.

Upvotes: 1

Ali.Ghodrat
Ali.Ghodrat

Reputation: 3704

In Windows 10,

Step 1: Download Dos2Unix from here:

https://waterlan.home.xs4all.nl/dos2unix.html#DOS2UNIX

Enter image description here

Step 2: Unzip downloaded folder

Enter image description here

Step 3: Copy unzipped folder

Step 4: Go to C:\Users\myusername ("myusername" is just an example here, you should write your username)

Step 5: Paste the folder.

Step 6: Open system environment variable settings:

Enter image description here

Step 7: Copy your bin path or just this line : C:\Users\myusername\dos2unix-7.4.2-win64\bin (change myusername with yours) and paste it into the new variable.

Enter image description here

Step 8: Done! just close and reopen command line or your IDE.

Upvotes: 1

uqi8
uqi8

Reputation: 49

You can use another "working" build-deploy dockerfile script like this.

FROM maven:3.8-openjdk-18 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package

FROM openjdk:18-alpine
COPY --from=build /usr/src/app/target/projectName-0.0.1-SNAPSHOT.jar /usr/app/projectName-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/app/projectName-0.0.1-SNAPSHOT.jar"]

You should change projectName with your jar file's name and prefer another maven version and jdk version.

Upvotes: 0

Alex Escu
Alex Escu

Reputation: 217

I used the following to fix the problem:

# clean up the file
RUN sed -i 's/\r$//' mvnw
# run with the SH path
RUN /bin/sh mvnw dependency:resolve

Upvotes: 5

Guilherme Carvalho
Guilherme Carvalho

Reputation: 73

I just found the solution! Every modern IDE has the option to change the line ending format.

The print screen of the dam option

Just select the whole code, and change it to the Mac OS format.

Solved.

~at least for me, it worked as a charm

Upvotes: 2

my zhang
my zhang

Reputation: 1

I try to use Uyric and helvete's solution, then my Dockerfile work.

yum install dos2unix
dos2unix mvnw

Upvotes: 0

Uyric
Uyric

Reputation: 743

I was facing the same issue when building an image based on openjdk:14-alpine from a Windows 10 machine. The issue was that locally the mvnw script had Windows line endings (\r\n called CRLF).

I've tried calling the script with /bin/sh mvnw but I would still have problems. The first solution was to convert the line endings to Linux EOL (\n called "LF") from my IDE.

A better approach is to install dos2unix and run dos2unix mvnw just before calling the script. On Linux Slim (Debian) you may do apt-get update && apt-get install dos2unix.

You may also try chmod +x mvnw to make the script executable.

Upvotes: 40

Thiago de Souza Rios
Thiago de Souza Rios

Reputation: 171

Try cleaning your mvnw file by removing spaces and CR characters from the file, using:

sed -i 's/\r$//' mvnw

I was able to execute the mvn script inside the container after using this command.

Upvotes: 17

Related Questions