Oliver Watkins
Oliver Watkins

Reputation: 13519

Problem building gcloud project : COPY failed: no source files were specified

I have a java project with a DockerFile that looks like that:

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} shape-shop-backend.jar
ENTRYPOINT ["java","-jar","/shape-shop-backend.jar"]

If I run docker like this :

C:\dev\shape-shop>docker build -t testtest:1.0

It builds an image perfectly.

I am under the assumption that "gcloud builds submit" works in the same way. I have a project on the google cloud called whataboutanewproject, so in the same directory I run this command :

C:\dev\shape-shop>gcloud builds submit --tag gcr.io/whataboutanewproject/shapeshop

But I get this error :

Digest: sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3
Status: Downloaded newer image for openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/6 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Running in 36cf2156a797
Removing intermediate container 36cf2156a797
 ---> 7320b32f6fe1
Step 3/6 : USER spring:spring
 ---> Running in f439aa9ca30e
Removing intermediate container f439aa9ca30e
 ---> 67f952c7bc2a
Step 4/6 : ARG JAR_FILE=target/*.jar
 ---> Running in 0bcfbf5f3b79
Removing intermediate container 0bcfbf5f3b79
 ---> 7519fa3bdc2f
Step 5/6 : COPY ${JAR_FILE} shape-shop-backend.jar
COPY failed: no source files were specified
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ERROR: (gcloud.builds.submit) build d3704337-88de-4dda-a120-ebd2a01ee8c9 completed with status "FAILURE"

C:\dev\shape-shop>

Why can it not process the DockerFile properly?

Does "gcloud build submit" even build an image? The documentation on the website is such utter garbage, that I have no clue actually what this "gcloud build submit" command even does.

Upvotes: 1

Views: 1132

Answers (2)

jbrems
jbrems

Reputation: 101

I struggled with a similar issue and discovered in the documentation for gcloud builds submit that if you don't have a .gcloudignore file your .gitignore file will be used which caused the issue for me.

Upvotes: 10

DazWilkin
DazWilkin

Reputation: 40296

You must specify a source location.

gcloud builds submit ${PWD} ... will then copy e.g. ${PWD} (commonly . is used instead) to the /workspace (default) directory within Cloud Build so that the build (specifically COPY ${JAR_FILE} ... can proceed.

Example

FROM busybox
ARG JAR_FILE=target/test.txt
COPY ${JAR_FILE} test.txt
ENTRYPOINT ["more","test.txt"]

NOTE Better not to copy wildcards to a specific file

And:

.
├── Dockerfile
└── target
    └── test.txt

And:

docker build --tag=test --file=./Dockerfile .
docker run --interactive --tty test:latest
Hello Freddie!

Then, assuming a GCP Project ({PROJECT}) with Cloud Enabled:

gcloud builds submit . --tag=gcr.io/${PROJECT}/test --project=${PROJECT}
docker run --interactive --tty gcr.io/${PROJECT}/test:latest
...
Hello Freddie!

NOTE gcloud builds submit requires . or ${PWD}

Upvotes: 1

Related Questions