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.
.*ignorefiles (.gitignore, ..cloudignoreand.dockerignore)..gitignoreshould not impact Cloud Build..cloudignorecan be used to inhibit files being copied to the Cloud..dockerignorecan be used to inhibit files being included in adocker 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/workspacefolder (Cloud Build's staging folder) before thedocker buildstep e.g.ls -la /workspace.WORKDIRin a Dockerfile, theWORKDIRfolder is used as the working directory (folder) so it's redundant (and thus confusing) to precedeCOPY . /app/somethingwithWORKDIR /appand you couldCOPY . 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", ...].[....]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.!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).<---- FAILS HERE. You could add aRUN ls -la ${JAR_FILE}before that line too. Presumably at least one of yourmvncommands is failing. Do you have the build output from those steps?