0

I am new to Docker and i want to build my jar file in a container using command mvnw package but i am getting this error. ERROR [3/3] RUN ./mvnw package 0.5s ------ > [3/3] RUN ./mvnw package: #7 0.448 /bin/sh: 1: ./mvnw: not found ------ executor failed running [/bin/sh -c ./mvnw package]: exit code: 127

I have java version 8 installed from OpenJDK image.

This is my Dockerfile

FROM openjdk:8

WORKDIR /usr/src/app

COPY . .

RUN ./mvnw package

CMD ["java", "-jar", "./target/springjava.jar"]

and my directory is Directory Structure

Using maven image it works as expected

FROM maven:3.6.3-jdk-8

WORKDIR /usr/src/app

COPY . .

RUN mvn clean package

CMD ["java", "-jar", "./target/springjava.jar"]

SOLUTION : On windows system you have to "Convert the mvnm script file to Unix format by removing carriage return characters".

FROM openjdk:8

WORKDIR /usr/src/app

COPY . . 

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

RUN ./mvnw package

CMD ["java", "-jar", "./target/springjava.jar"]
  • Either the path to the executable is wrong or the image you use just does not have maven installed. – deHaar Aug 04 '23 at 12:20
  • You might want to try giving yourself the permission to access the file with `chmod`. Aside from that, I am unsure about `COPY . .`. Try using the full target path instead, e.g. `COPY . /app` (assuming the `WORKDIR` is set to `/app`). If you're installing Maven via `apt-get`, you'd need to use `mvn` and not `./mvnw`. – dan1st Aug 04 '23 at 12:23
  • @dan1st tried using chmod to give permission but gives mvnw does not exist. I used CMD ["ls", "-l"] to list all files and i can see mvnw ``` -rwxr-xr-x 1 root root 70 Aug 4 13:01 Dockerfile -rwxr-xr-x 1 root root 259 Aug 3 19:09 README.md -rwxr-xr-x 1 root root 9399 Aug 3 19:09 mvnw -rwxr-xr-x 1 root root 5971 Aug 3 19:09 mvnw.cmd -rwxr-xr-x 1 root root 1834 Aug 3 19:09 pom.xml drwxr-xr-x 4 root root 4096 Aug 4 07:24 src ``` – Yagami Ackerman Aug 04 '23 at 13:03
  • could you just package the jar from your IDE and copy it to your docker image when building instead? – experiment unit 1998X Aug 04 '23 at 13:16
  • 1
    Can you try changing `COPY . .` to `COPY . /app` and add `WORKDIR /app` right after the `COPY` command? – dan1st Aug 04 '23 at 13:28
  • @dan1st tried that but not able to run mvnw command to build the jar, it has to be run from a bash env and I dont know how to do it. – Yagami Ackerman Aug 04 '23 at 14:03
  • @experimentunit1998X i want to build the jar inside the container and then run it. Issue is i am not able to use mvnw package command – Yagami Ackerman Aug 04 '23 at 14:15
  • What makes you think you'd need to run it from a bash env? – dan1st Aug 04 '23 at 14:16
  • If you really insist on doing that, then you can just use a docker image provided by maven to build your jar inside the docker image. See [here](https://gauthier-cassany.com/posts/build-jar-inside-docker) and the relevant [docker image](https://hub.docker.com/layers/library/maven/3.6.3-jdk-8/images/sha256-09c4bf58d7e7e31bdc1ab73ef10349b3a47c9a814e715e409034fb3293253ce2) from maven – experiment unit 1998X Aug 04 '23 at 14:27
  • @dan1st I ran the command `./mvnw package` from my local git bash it created the jar file. – Yagami Ackerman Aug 04 '23 at 14:45
  • @experimentunit1998X Using maven:3.6.3-jdk-8 works fine. Thanks. But I have to build the jar with this command "./mvnw package". – Yagami Ackerman Aug 04 '23 at 14:49
  • @experimentunit1998X that was a mistake, i ran both ways just to see – Yagami Ackerman Aug 04 '23 at 14:54
  • could take a look at this? https://stackoverflow.com/a/59214879/16034206 – experiment unit 1998X Aug 04 '23 at 15:01
  • @experimentunit1998X yes it worked, thank you so much. I had to include this ``RUN sed -i 's/\r//' mvnw`` before running mvnw command – Yagami Ackerman Aug 04 '23 at 15:19

0 Answers0