0

Configuring the GitHub Action to build a image of this project I am getting this error:

Step 5/7 : COPY /target/*.jar app.jar
COPY failed: no source files were specified

This is the project Dockerfile, and I am referencing the file from build context path, not from relative path.

FROM openjdk:8-jdk-alpine as build
COPY . /usr/app
WORKDIR /usr/app
RUN chmod +x mvnw \
    && ./mvnw --version \
    && ./mvnw clean package
COPY /usr/app/target/*.jar app.jar
EXPOSE 8080

ENTRYPOINT ["java","-jar","app.jar"]

I already verified if the file exist and read permissions with ls -l /target command:

drwxr-xr-x    3 root     root          4096 Oct 18 15:35 classes
drwxr-xr-x    3 root     root          4096 Oct 18 15:35 generated-sources
drwxr-xr-x    3 root     root          4096 Oct 18 15:35 generated-test-sources
-rw-r--r--    1 root     root      48625321 Oct 18 15:35 learning-spring-boot-0.0.1.jar
-rw-r--r--    1 root     root         33089 Oct 18 15:35 learning-spring-boot-0.0.1.jar.original
drwxr-xr-x    2 root     root          4096 Oct 18 15:35 maven-archiver
drwxr-xr-x    3 root     root          4096 Oct 18 15:35 maven-status
drwxr-xr-x    2 root     root          4096 Oct 18 15:35 surefire-reports
drwxr-xr-x    3 root     root          4096 Oct 18 15:35 test-classes

And this is the output with find . after the ./mvnw clean package command:

./target/learning-spring-boot-0.0.1.jar
./target/maven-archiver
./target/maven-archiver/pom.properties
./target/generated-sources
./target/generated-sources/annotations
./target/surefire-reports

Locally runs using the relative path, but fails with path from build context.

COPY /target/*.jar app.jar

Here is the project url.

What I am missing?

2
  • Have you tried building this dockerfile locally, not on github? Is the app.jar committed and in the git repo with the correct permissions? Try building locally on a fresh clone of the repo. Commented Oct 18, 2021 at 16:21
  • Yes, sorry, see my update. Let me check for repository permissions Commented Oct 18, 2021 at 16:39

2 Answers 2

1
+50

It looked to me like you wanted to use a multi-stage docker build, but you did not start the second stage. The second COPY step is not working because it is still in the same stage. And like BMitch mentioned, within the same stage, you can copy only from the build context using COPY. I made a couple of changes(started second stage, used --from in COPY) to your Dockerfile and it builds fine now. Dockerfile contents below:

FROM openjdk:8-jdk-alpine as build
COPY . /usr/app
WORKDIR /usr/app
RUN chmod +x mvnw \
    && ./mvnw --version \
    && ./mvnw clean package

FROM openjdk:8-jre-alpine
COPY --from=build /usr/app/target/*.jar app.jar
EXPOSE 8080

ENTRYPOINT ["java","-jar","app.jar"]
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, I understand better now, by the way this multistage build reduce a lot my image size ~70 MB, excellent
0

ADD includes content from your build context into the image, it does not copy files within your image. For the latter, just do a RUN cp ... command, e.g.

FROM openjdk:8-jdk-alpine as build
COPY . /project
WORKDIR /project
RUN chmod +x mvnw \
 && ./mvnw --version \
 && ./mvnw clean package \
 && cp ./target/learning-spring-boot-0.0.1.jar app.jar
EXPOSE 8080

ENTRYPOINT ["java","-jar","app.jar"]

Since you changed WORKDIR to be /project, that shouldn't be included in the cp unless you use an absolute path.

1 Comment

I still receive the same kind of error: cp: can't stat './project/target/learning-spring-boot-0.0.1.jar': No such file or directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.