'Can't run gradle wrapper with docker build

I have this small build:

FROM eclipse-temurin:17-jdk as build-java
COPY java-project /root/project
WORKDIR /root/project
RUN chmod +x ./gradlew
RUN ls
RUN ./gradlew ...
#9 [build-build-java 5/6] RUN ls
#9 sha256:2d05b1e31ea4eaa8a9343b4f48fadcf7351b9d55dcc9c27e572d7bb32c4d723c
#9 0.194 build
#9 0.194 build.gradle
#9 0.194 docker-compose.yml
#9 0.194 gradle
#9 0.194 gradlew
#9 0.194 gradlew.bat
#9 0.194 HELP.md
#9 0.194 lombok.config
#9 0.194 settings.gradle
#9 0.194 src
#9 DONE 0.2s

#10 [build-build-java 6/6] RUN ./gradlew ...
#10 sha256:62cb4a1a6288008f3faccc3504e63ef87d30c3d7fde904a0576ffcf571e9b310
#10 0.310 /bin/sh: 1: ./gradlew: not found
#10 ERROR: executor failed running [/bin/sh -c ./gradlew ...]: exit code: 127

I've bene stuck on this for a while now.

I've tried using FROM gradle instead and then using gradle ... but then I get this file mount error:

#10 sha256:26de7798a76971c3eb12e6398f830ee487fe41c110d0f8ca6a23a92ee5437267
#10 0.854 
#10 0.854 Welcome to Gradle 7.3.3!
#10 0.854
#10 0.854 Here are the highlights of this release:
#10 0.854  - Easily declare new test suites in Java projects
#10 0.855  - Support for Java 17
#10 0.855  - Support for Scala 3
#10 0.855
#10 0.855 For more details see https://docs.gradle.org/7.3.3/release-notes.html
#10 0.855
#10 0.952 Starting a Gradle Daemon (subsequent builds will be faster)
#10 2.153 Unable to list file systems to check whether they can be watched. Assuming all file systems can be watched. Re
ason: Could not query file systems: could not open mount file (errno 2: No such file or directory)



Solution 1:[1]

/bin/sh: 1: ./gradlew: not found is usually caused by carriage return (\r) line endings in the bash script. Removing the carriage return will most like resolve the issue.

Solution 2:[2]

Got exact same problem on Windows environment. This has to do with the line endings of the shell script files.

I fixed it this way:

  1. Step - set the line endings to match the *nix style
git config --global core.autocrlf input
  1. Delete the git repo and re-clone it using the updated configuration

  2. Relaunch the docker build

NB! just setting the configuration without re-cloning the repo doesn't help.

Solution 3:[3]

I was having the same problem with the Windows environment. This happens according to carriage return (\r) line endings in the bash script as @thokuest pointed out. But you should consider when you changed the line ending to LF from CLRF in IntelliJ, the gradlew file in the container is not changed accordingly and it is cached from your previous docker build . command. So you should run this command:

docker build --no-cache .

or:

docker build --no-cache --progress=plain . if you want to see full output.

Solution 4:[4]

Converting EOL to UNIX (LF) of the gradlew file should work. It can be fixed using notepad++.

  • Open the gradlew file in notepad++

  • Edit -> EOL Conversion -> UNIX (LF)

  • save

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 thokuest
Solution 2
Solution 3 MHM
Solution 4 ranjan