Issue
While trying to create docker build, getting error as below:
Status: Downloaded newer image for adoptopenjdk/openjdk11:alpine-jre
---> fcb7960593e0
Step 13/18 : ENV ARTIFACT_NAME=userservice.jar
---> Running in f9cb2c5751ce
Removing intermediate container f9cb2c5751ce
---> a8d457c0023a
Step 14/18 : ENV APP_HOME=/usr/app/
---> Running in 4376cb0c3120
Removing intermediate container 4376cb0c3120
---> 6df0ab62c566
Step 15/18 : WORKDIR $APP_HOME
---> Running in e44347ce7a6b
Removing intermediate container e44347ce7a6b
---> ac63acfe40e6
Step 16/18 : COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/*.jar ${ARTIFACT_NAME}
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
Here is the docker file:
# using multistage docker build
# ref: https://docs.docker.com/develop/develop-images/multistage-build/
# temp container to build using gradle
FROM gradle:6.9.1-jdk11 AS TEMP_BUILD_IMAGE
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY build.gradle settings.gradle $APP_HOME
COPY gradle $APP_HOME/gradle
COPY --chown=gradle:gradle . /home/gradle/src
USER root
RUN chown -R gradle /home/gradle/src
RUN gradle build || return 0
COPY . .
RUN gradle clean build
# actual container
FROM adoptopenjdk/openjdk11:alpine-jre
ENV ARTIFACT_NAME=userservice.jar <--- Here (fails)
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/*.jar ${ARTIFACT_NAME} <--- Here (fails)
EXPOSE 8080
ENTRYPOINT ["java", "-Dspring.profiles.active=h2_db", "-jar", "${ARTIFACT_NAME}"]
If we hardcode build file name as below, then things works fine:
# actual container
FROM adoptopenjdk/openjdk11:alpine-jre
ENV ARTIFACT_NAME=userservice-0.0.2-SNAPSHOT.jar <--- Here (works)
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/$ARTIFACT_NAME . <--- Here (works)
How to copy the latest build artifacts as a specific name? In above case userservice-0.0.2-SNAPSHOT.jar should be picked as userservice.jar.
Solution
When you define ENV APP_HOME=/usr/app/
, the final COPY source
becomes /usr/app//build/libs/*.jar
.
It looks for wildcard, docker build can't handle it well. But if you don't use wildcard, above //
works well.
So, for your scenario, just change to ENV APP_HOME=/usr/app
could fix your problem.
Dockerfile:
FROM gradle:6.9.1-jdk11 AS TEMP_BUILD_IMAGE
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
RUN mkdir -p build/libs; cd build/libs; touch userservice-0.0.2-SNAPSHOT.jar
FROM adoptopenjdk/openjdk11:alpine-jre
ENV ARTIFACT_NAME=userservice.jar
#ENV APP_HOME=/usr/app/
ENV APP_HOME=/usr/app
WORKDIR $APP_HOME
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/*.jar ${ARTIFACT_NAME}
EXPOSE 8080
ENTRYPOINT ["java", "-Dspring.profiles.active=h2_db", "-jar", "${ARTIFACT_NAME}"]
Execution:
# docker build . --no-cache
Sending build context to Docker daemon 2.048kB
......
Step 9/11 : COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/*.jar ${ARTIFACT_NAME}
---> d9a7219be751
Step 10/11 : EXPOSE 8080
---> Running in df81e955b4b9
Removing intermediate container df81e955b4b9
---> c3f019d65533
Step 11/11 : ENTRYPOINT ["java", "-Dspring.profiles.active=h2_db", "-jar", "${ARTIFACT_NAME}"]
---> Running in 7c62bf624cb1
Removing intermediate container 7c62bf624cb1
---> e5377e14375d
Successfully built e5377e14375d
Additional, if you change backend to BUILDKIT
like next, the /usr/app/
still can work, so I guess this related to docker implemenation issue:
Execution with buildkit:
# DOCKER_BUILDKIT=1 docker build --progress=plain . --no-cache
......
#10 [stage-1 3/3] COPY --from=TEMP_BUILD_IMAGE /usr/app//build/libs/*.jar us...
#10 DONE 0.1s
#11 exporting to image
#11 exporting layers 0.0s done
#11 writing image sha256:730755ba444c13f289614b64d5cfd3be900b94c8d668307477ed2908183df14f
#11 writing image sha256:730755ba444c13f289614b64d5cfd3be900b94c8d668307477ed2908183df14f done
#11 DONE 0.0s
Answered By - atline