Dockerfile Add command failing to locate target folder

Viewed 935

I have a Dockerfile in the root directory of a Spring Boot application and also I have a target folder in the same path as shown in below structure:

|--src
  |--main
     |--com.abc.myApp
|--target/xyz.jar
|--Dockerfile
|--pom.xml

My Dockerfile looks as below:

From openjdk:8-jdk-alpine
WORKDIR /opt
ADD /target/xyz.jar .

When I try to execute using following command:

docker build . -t xyz

I am getting error as below:

Step 3/3 : ADD /target/xyz.jar .
ADD failed: stat /var/lib/docker/tmp/docker-builder225663872/target/xyz.jar: no such file or directory

The Dockerfile is present at /opt/MyApp/Dockerfile and target folder is present at /opt/MyApp/target/xyz.jar

Where am I going wrong? Also I do not have any .dockerignore file.

2 Answers

As the the jar file will also contain the version number you better use this solution

FROM openjdk:8-jdk-alpine

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar

ENTRYPOINT ["java","-jar","/app.jar"]

ARG is there because COPY doesn't work with wildcards.

And as already commented you have to call first:

mvn package

Please read more about Spring Boot and Docker: https://spring.io/guides/gs/spring-boot-docker/

Starting with Spring 2.3 you even don't need a Dockerfile. You can run

mvn spring-boot:build-image

Also read this: https://www.baeldung.com/dockerizing-spring-boot-application

Pay attention where you are building your images. For example, If you are in your home directory :-

/home/user/

and your have your Dockerfile and jar placed in the same directory. Your Docker file should have the following entry :-

FROM adoptopenjdk/openjdk11:alpine-jre
ADD item-0.0.1-SNAPSHOT.jar itemapp.jar
ENTRYPOINT ["java","-jar","itemapp.jar"]

Add should not have the /target path.

Related