15

I'm new to Docker (but not to developing).

I have this Dockerfile:

#
# Build stage
#
FROM maven:3.6.3-jdk-8-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package

#
# Package stage
#
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG war_FILE=/home/app/target/*.war
COPY ${war_FILE} app.war
ENTRYPOINT ["java","-jar","/app.war"]

And (so far so good) the build succeeds:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:51 min
[INFO] Finished at: 2020-01-15T08:43:54Z
[INFO] ------------------------------------------------------------------------

but in the end, the docker build fails:

Removing intermediate container 7c66e8b7dbed
---> 74b1c50c84ad
Step 5/10 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 6/10 : RUN addgroup -S spring && adduser -S spring -G spring
---> Running in ca236cf9a705
Removing intermediate container ca236cf9a705
---> 0c255ef5868f
Step 7/10 : USER spring:spring
---> Running in 8452dcff6a8a
Removing intermediate container 8452dcff6a8a
---> 6265f412699a
Step 8/10 : ARG war_FILE=/home/app/target/*.war
---> Running in 3b54067b2cca
Removing intermediate container 3b54067b2cca
---> c186c4a7e443
Step 9/10 : COPY ${war_FILE} app.war
COPY failed: no source files were specified

I'm probably doing something incredibly stupid but I don't know where to start looking...

banan3'14
  • 3,810
  • 3
  • 24
  • 47
Sander Theetaert
  • 153
  • 1
  • 1
  • 7

4 Answers4

11

On a multistage build, data of each stage is not shared among stages.

Thus, indeed on build stage you have successfully created the war file under /home/app/target/.

However, on second stage this path does not exist, resulting in the reported error.

To solve this, replace second stage with:

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
COPY --from=build /home/app/target/war_name.war app.war
ENTRYPOINT ["java","-jar","/app.war"]

Example from official docs.

leopal
  • 4,711
  • 1
  • 25
  • 35
7

Check the .gitignore and .dockerignore file

  1. Remove the /target(source file path from there you are copying) from them.
  2. Save the changes.
  3. Run the docker build command

Note : some time the source path is mentioned in the ignore file and our build command ignore that.

Prabhat Yadav
  • 1,181
  • 6
  • 18
  • 29
1

In your Dockerfile,

ARG war_FILE=/home/app/target/*.war
COPY ${war_FILE} app.war

Due to some reason, the ${war_FILE} is not getting updated. Can you check whether .war files are present at the /home/app/target/ after the build?

Also, if the war_FILE is a constant, why not directly copying the files from that location? i.e.

COPY /home/app/target/*.war app.war

Again, if there will be multiple .war files this can be a problem

Anuradha Fernando
  • 1,063
  • 12
  • 17
0

I encountered the same issue and resolved my issue by ensuring the file name and directories were spelled correctly and matched casing. File names and directories are case sensitive and you'll likely encounter the error described above if they don't match, including casing.

BDarley
  • 697
  • 1
  • 10
  • 19
  • 1
    I still get the error if I use ARG variables in the COPY command. I can echo the variable out, copy and paste it directly into the COPY command and it works. Here is how I typed the command. COPY [${projectFilePath}, "./"] But, COPY ["explicit path", "./"] works fine. – Dranyar Feb 28 '22 at 16:39
  • Ok, quick follow-up. It turns out you cannot use the []'s if you want to supply an ARG variable. So, COPY ${projectFilePath} "./" works. – Dranyar Feb 28 '22 at 16:42