0

I'm having a strange problem where a Dockerfile builds completely fine locally but fails in GCP cloud build due to being unable to find the source files during a copy statement. I am building a spring jar file from mvn clean package which I can see is successfully completing in the logs and is overwriting the appropriate package within the target/ directory.

Dockerfile

**FROM openjdk:21-slim
WORKDIR /app
COPY . /app/my-project/
# Update the package lists
RUN apt-get update -y
# Install Maven
RUN apt-get install -y default-jdk maven
WORKDIR /app/my-project
RUN ["mvn", "clean", "compile", "-Dspring-boot.run.jvmArguments='-Dspring.profiles.active=stage'"]
RUN ["mvn", "clean" ,"package" ,"-DskipTests=true"]
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} /etc/my-project/app.jar <---- FAILS HERE
EXPOSE 8080
ENTRYPOINT ["java", "-Dspring.profiles.active=stage", "-jar","/etc/my-project/app.jar"]

cloudbuild.yaml

steps:
- name: gcr.io/cloud-builders/docker
  args: ['build', '-t', 'prod/my-project', '.']
options:
  logging: CLOUD_LOGGING_ONLY

.cloudignore This is the latest thing I've tried, since I read somewhere that what is in my .gitignore is somehow being excluded from my cloud build. Having a .cloudignore files supposedly helps in including the target directory but I have not been successful with it.

!target/

Edit .cloudignore I've also tried the following as I saw it in another post with no success either.

!target/**

Failure reason

"COPY failed: no source files were specified"

Log of mvn clean package succeeding

Replacing main artifact /app/my-project/target/my-project-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies in BOOT-INF/."

I've tried everything I've typed above. I've also tried using a absolute path to the jar file itself with the same type of copy error.

7
  • How are you invoking Cloud Build? What is the source file structure? There are 3 .*ignore files (.gitignore, ..cloudignore and .dockerignore). .gitignore should not impact Cloud Build. .cloudignore can be used to inhibit files being copied to the Cloud. .dockerignore can be used to inhibit files being included in a docker build. It would be helpful if you include any of these. You can add debugging steps to Cloud Build using e.g. Bash to enumerate the content of the /workspace folder (Cloud Build's staging folder) before the docker build step e.g. ls -la /workspace. Commented Nov 29, 2024 at 16:58
  • Nits: 1. When you use WORKDIR in a Dockerfile, the WORKDIR folder is used as the working directory (folder) so it's redundant (and thus confusing) to precede COPY . /app/something with WORKDIR /app and you could COPY . something; 2. This one's more controversial but, I think it's better to bind flags to values so [.., "--tag=prod/my-project",...] rather than [..., "-t", "prod/my-project", ...]. Commented Nov 29, 2024 at 17:04
  • It is "convention" but [....] is JSON and, because YAML>JSON, this is perfectly fine but, if you were to pump the result through a YAML linter, it would convert this to pure YAML which (I also think) is preferable. Commented Nov 29, 2024 at 17:08
  • @DazWilkin my .gitignore is standard auto-generated one in vscode. It excludes /target which is the most notable part of it but I re-include /target within my .gcloudignore using !target/** without much success. I'll see about using bash to log the directories and take a peek at what is happening. For the time being I removed the copy statement and attempted to just use the JAR_FILE arg directly in the ENTRY command which at the least succesfully builds now - but there is something definitely horrendously wrong given my images don't build into my repo either (my repo link is not in above). Commented Nov 29, 2024 at 17:45
  • I just noticed your <---- FAILS HERE. You could add a RUN ls -la ${JAR_FILE} before that line too. Presumably at least one of your mvn commands is failing. Do you have the build output from those steps? Commented Nov 29, 2024 at 18:06

1 Answer 1

0

Solved by creating a multi-stage Dockerfile which uses the initial stage as the builder and copies over the files from that stage.

Sign up to request clarification or add additional context in comments.

Comments

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.