16

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hansen
  • 650
  • 1
  • 11
  • 32
  • could you try like, "RUN chmod 700 mvnw && mvnw clean install package ==>error on this line" without "./" – Janitha Madushan Apr 15 '20 at 10:38
  • @JMadushan : i already try it, but got /bin/sh: 1: mvnw: not found – Hansen Apr 15 '20 at 10:40
  • I guess you are not copying maven wrapper (mvnw) to the image. you could run "RUN ls" after the line "WORKDIR /src" just to inspect that. – Janitha Madushan Apr 15 '20 at 10:57
  • 1
    @JMadushan : after i add run ls , i get the list of file as below, Compiler, Dockerfile, mvnw, mvnw.cmd, pom.xml, src, target, is this mean something? sorry i newbie in docker, – Hansen Apr 16 '20 at 02:36
  • You may have a look at [Dockerfile : /bin/sh: 1: ./mvnw: not found error](https://stackoverflow.com/questions/74597445/dockerfile-bin-sh-1-mvnw-not-found-error/75234575#75234575) for workaround. – Murat Yıldız Jan 25 '23 at 13:19

14 Answers14

35

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.

helvete
  • 2,455
  • 13
  • 33
  • 37
Uyric
  • 646
  • 7
  • 17
  • Can anyone provide me a pointer that where should I add the **apt-get update && apt-get install dos2unix** in the docker file. I did not able to fully understand the solution. – Sameed Tariq Aug 04 '22 at 12:14
  • This question is about running `./mvnw` in a Linux container, for code written in Windows. You need to install the package and run it on the "mvnw" file before trying to execute it (e.g. line 6 in the OP's code). – Uyric Aug 08 '22 at 12:22
  • `\r` and `\n` do not map cleanly to CR and LF. – Peter Mortensen Mar 16 '23 at 20:16
  • See e.g. [this comment](https://stackoverflow.com/questions/12747722/what-is-the-difference-between-a-line-feed-and-a-carriage-return/12747850#comment129299456_12747850): *" \n is not a character. You will not find it in ASCII, ANSI, Latin-8, UTF-8 etc. It is a programming language-specific escape sequence, originating from the language C. And it NEVER means LF. It stands for the escape-sequence representing the platform's (OS) control character for new-line."* – Peter Mortensen Mar 16 '23 at 21:03
13

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.

3

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
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Alex Escu
  • 147
  • 1
  • 6
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richie
  • 570
  • 6
  • 10
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali.Ghodrat
  • 3,348
  • 3
  • 31
  • 31
1

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

bguiz
  • 27,371
  • 47
  • 154
  • 243
1

Install Bash in your Docker image.

#!/bin/bash
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
natedennis
  • 117
  • 8
0

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'"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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

yum install dos2unix
dos2unix mvnw
my zhang
  • 1
  • 1
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '22 at 13:52
  • Add `--no-cache` to what? – Peter Mortensen Mar 16 '23 at 21:15
  • This worked for me. I ran `docker build . --no-cache` and the error went away. – kyle_aoki Apr 21 '23 at 04:13
0

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.

uqi8
  • 29
  • 7
0

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Ling
  • 1
  • 2
0

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.

user10942209
  • 11
  • 1
  • 3