Working with Java Spring Boot and Docker, I kept hitting this frustrating error during build:
/bin/sh: ./gradlew: not found
No matter what I tried, Docker just couldn’t run the Gradle wrapper inside the container. Turns out the culprit was something subtle — Windows CRLF line endings on gradlew
.
What’s going on?
- When you build a Docker image with a Java project using Gradle, the
gradlew
file must be copied into the container and be executable. - But if you’re on Windows,
gradlew
often has CRLF (Carriage Return + Line Feed) endings instead of Unix’s LF endings. - Linux-based containers can’t execute scripts with Windows line endings properly, so it throws a “not found” error.
- This problem is especially sneaky because your file looks fine on Windows and even inside Docker it shows up — but it silently fails when run.
How to confirm the issue?
- Check line endings in your editor (VSCode, Notepad++ show line endings).
- Run
file gradlew
on Linux — if it showsCRLF
line endings, that’s the issue. - Add
RUN ls -la
afterCOPY
in Dockerfile to confirmgradlew
is copied. - Try running
chmod +x ./gradlew
— no errors but still./gradlew: not found
.
How to fix it?
Option 1: Fix on your host machine (best)
- Use tools like
dos2unix gradlew
locally before building. - Configure your Git to checkout with LF line endings:
git config --global core.autocrlf input
- Set your editor to save files with LF endings.
Option 2: Fix inside Dockerfile (if host fix is impossible)
RUN apk add --no-cache dos2unix \
&& dos2unix ./gradlew \
&& chmod +x ./gradlew
Final Dockerfile snippet example
COPY . .
RUN apk add --no-cache dos2unix \
&& dos2unix ./gradlew \
&& chmod +x ./gradlew
RUN ./gradlew bootJar --no-daemon -x test
Conclusion
This tiny line endings mismatch wasted hours of debugging for me, but now you know the trick to avoid it.
Make sure to:
- Check your line endings,
- Convert them properly,
- And always test inside your container early.
This simple fix will save you headaches and speed up your Docker + Gradle builds!
Top comments (0)