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

I try to build maven project using dockerfile, but i got this error when i use command docker build

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

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

but still got 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  ==>error on this line

# Stage 2 Run the jar file from 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? I would be glad for any help.



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

Absolutely, if the file has a line ending of windows format it will not be parsed as Linux file, therefore, it will result into not found file, to solve it install sudo apt-get install dos2unix and run dos2unix mvnwthis convert the file into Linux line ending type so it is recognized as Linux file.

Solution 4:[4]

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.

Solution 5:[5]

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

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

Solution 6:[6]

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

yum install dos2unix
dos2unix mvnw

Solution 7:[7]

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

Solution 8:[8]

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

Solution 9:[9]

install bash in your docker image.

#!/bin/bash

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 helvete
Solution 2 Thiago de Souza Rios
Solution 3 Richie
Solution 4
Solution 5 Tomerikoo
Solution 6 my zhang
Solution 7 bguiz
Solution 8 Horse Zhu
Solution 9 natedennis