Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COPY or ADD Command in Dockerfile Fails to Find Jar File for Springboot Application

Tags:

java

docker

maven

I am getting this error when I am trying to copy the generated jar file from the target folder to /usr/share/ folder in the Docker image. I have scoured Docker forum sites and people are having the exact same issue but there are no clear answer that solves this problem.

Step 9/10 : COPY target/${JAR_FILE} /usr/share/${JAR_FILE}
ERROR: Service 'myservice' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder558103764/target/myservice.jar: no such file or directory

Here's my Dockerfile: ----------------------- begin ---------

FROM adoptopenjdk/openjdk11:alpine
MAINTAINER XXX XXX <[email protected]>

# Add the service itself
ARG JAR_FILE="myservice-1.0.0.jar"
RUN apk add maven
WORKDIR /app
COPY . /app/
RUN mvn -f /app/pom.xml clean install -DskipTests
WORKDIR /app
COPY target/${JAR_FILE} /usr/share/${JAR_FILE}

ENTRYPOINT ["java", "-jar", "/usr/share/myservice-1.0.0.jar"]
------ end snip -------

Here's how I run this from the base folder where I have my Dockerfile on my Mac.

docker build -t service-image .
like image 826
ram mil Avatar asked Nov 02 '25 06:11

ram mil


1 Answers

So I guess you are trying to move your src to image and run a mvn build and copy built file from target to share folder.

If so, every thing seems to be fine except this line

COPY target/${JAR_FILE} /usr/share/${JAR_FILE}

COPY takes in a src and destination. It only lets you copy in a local file or directory from your host (the machine building the Docker image) into the Docker image itself

I think your intention is to copy file inside your container's /target to /usr/share folder. try this

RUN cp target/${JAR_FILE} /usr/share/${JAR_FILE}

Regrading error which you see its because with COPY command Docker will try to get the file from docker default path in your HOST

i.e /var/lib/docker/tmp/docker-builder558103764/

where /target folder doesn't exist

like image 137
MyTwoCents Avatar answered Nov 03 '25 19:11

MyTwoCents