How to include built maven (JAR) in docker build step

Viewed 42

I don't use docker multi-stages build.

I already built maven to JAR file with Jenkins pipeline.

mvn -f /home/app/pom.xml clean package -Dmaven.test.skip

Result file located at /home/app/target/myfile.jar

After that i want to include this JAR file while build docker image.

I use this command in dockerfile.

COPY /home/app/target/myfile.jar /myfile.jar

But i got error when build docker.

COPY failed: stat /var/lib/docker/tmp/docker-builder199377670/home/app/target/myfile.jar: no such file or directory

I understand that absolute path is dynamic.

Please help suggest. How to specific location of JAR file.

Edit: Already moved file to same path with Dockerfile but still not work.

I tried to copy file with pipeline and verify that file is locate same path with Dockerfile.

2022-09-16 12:06:36  + cp /home/app/target/myapp-0.0.1-SNAPSHOT.jar sourceCode/myapp-0.0.1-SNAPSHOT.jar
2022-09-16 12:06:36  + cd sourceCode
2022-09-16 12:06:36  + ls
2022-09-16 12:06:36  Dockerfile
2022-09-16 12:06:36  myapp-0.0.1-SNAPSHOT.jar

And in Dockerfile, I add this copy command.

COPY /myapp-0.0.1-SNAPSHOT.jar /myapp-docker.jar

But when build docker. It still cannot find source file.

2022-09-16 12:06:53  Step 3/5 : COPY /myapp-0.0.1-SNAPSHOT.jar /myapp-docker.jar
2022-09-16 12:06:53  COPY failed: stat /var/lib/docker/tmp/docker-builder691591658/myapp-0.0.1-SNAPSHOT.jar: no such file or directory
1 Answers

The absolute path of your jar file must refer to an absolute path within the docker build context, not an absolute path of your host. All the resources you want to copy into docker image must refer to the location of Dockerfile.

If e.g. the Dockerfile is in /home folder then it is the root folder , i.e. / within the docker build contex and the COPY command will be:

COPY /app/target/xxxx.jar /path/to/appfolder/in/image/xxxx.jar

Check this page for further information.

Related