0

I have a simple Dockerfile in my spring boot as follow. I am able to build the image successfully locally, and can push using my credentials.

But my build keeps failing on every attempt to build automatically.

FROM openjdk:8-jdk-alpine
LABEL maintainer="xxxxx@xxx.com"
VOLUME /tmp
EXPOSE 8080
ARG JAR_FILE=target/jollof.jar
ADD ${JAR_FILE} jollof.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","- 
jar","/jollof.jar"]

From docker hub, I got this from the log.

Building in Docker Cloud's infrastructure...
Cloning into '.'...
Warning: Permanently added the RSA host key for IP address 'xxx.xx.xxx.xxx' to 
the list of known hosts.
....
....
Step 6/7 : ADD ${JAR_FILE} jollof.jar
ADD failed: stat /var/lib/docker/tmp/docker- 
  builder674045875/target/jollof.jar: 
no such file or directory
Motolola
  • 368
  • 5
  • 19

1 Answers1

3

Unlike your local environment, Docker Hub fetches then builds your project in a fresh environment, so that the file target/jollof.jar that is intended to be copied is not available in the docker context. Hence the error you observe.

So I'd suggest refactoring your Dockerfile so that mvn package or so is done in the Dockerfile itself (which is a best practice to adopt, for the sake of reproducibility). Note that this configuration will be working for Docker Hub's automated builds as well as the builds in your local environment.

For example, below is an example Dockerfile that inspired by the that of this SO answer How to convert a Spring-Boot web service into a Docker image? as well as the Dockerfile of your post:

FROM maven:3.6-jdk-8 as maven
WORKDIR /app
COPY ./pom.xml ./pom.xml
RUN mvn dependency:go-offline -B
COPY ./src ./src

# TODO: jollof-* should be replaced with the proper prefix
RUN mvn package && cp target/jollof-*.jar app.jar

# Rely on Docker's multi-stage build to get a smaller image based on JRE
FROM openjdk:8-jre-alpine
LABEL maintainer="xxxxx@xxx.com"
WORKDIR /app
COPY --from=maven /app/app.jar ./app.jar

# VOLUME /tmp  # optional
EXPOSE 8080    # also optional

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]
ErikMD
  • 13,377
  • 3
  • 35
  • 71
  • @ErkMD I completely missed the maven part in building the project, but I thought the idea of docker is to work the same way anywhere ... why did it work on my local environment? does it have access to my local maven installation? – Motolola Aug 18 '19 at 19:43
  • @IntegralMaster Once built, Docker images indeed ensure portability. But 2 images built from the same `Dockerfile` with different inputs (e.g., 2 different versions of your codebase) will always be different, or even couldn't compile. To sum up, when you do `docker build -t image-name .`, the last argument (here, current folder `.`) denotes what is called Docker's build context (the reference folder from which Docker can retrieve files when you use commands `COPY` or `ADD`). So if you do `ADD target/jollof.jar jollof.jar`, the outcome will be different if `./target/jollof.jar` exists or not. – ErikMD Aug 18 '19 at 19:58
  • I get it now. Basically, I had run 'mvn clean install' before building on my local, and this created a ./target/jollof.jar and this folder was missing on docker hub. – Motolola Aug 19 '19 at 07:54