简体   繁体   中英

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

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 <ramil.xxxxx@xxxx.ai>

# 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 .

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

ie /var/lib/docker/tmp/docker-builder558103764/

where /target folder doesn't exist

I had similar issue because of my .dockerignore had following exclusion:

target

Possible solutions are:

  • do not ignore target

  • define ignore exception

    !target/*.jar

What has also worked after scouring the Docker forums is using multi-stage build . The multi-stage build not only forces you to use the folders where you specify the WORKDIR in your build stage but also significantly reduces the size of your base images up to 5x-6x less than just using the one stage build like I have shown above. Below is what my solution to finding this jar in the folder I specified as my WORKDIR and use that jar file for my smaller base image. Single-stage build in my initial solution produces 560 MB image. This multi-stage Dockerfile below builds image that is only 108 MB .

FROM adoptopenjdk/openjdk11:alpine as compile
MAINTAINER XXXX <ramil.xxxx@xxxx.ai>

# Build the jar using maven 
RUN apk add maven
WORKDIR /app
COPY . /app/
RUN mvn -f pom.xml clean package -DskipTests

FROM adoptopenjdk/openjdk11:alpine-jre
# Copy the packaged jar app file to a smaller JRE base image
COPY --from=compile "/app/target/service-1.0.0.jar" /usr/share/

ENTRYPOINT ["java", "-jar", "/usr/share/service-1.0.0.jar"]

It turns out that the Dockerfile I was using wanted to be executed in a folder different from where the Dockerfile was located.

So given a build_config_folder/Dockerfile ...

So instead of

docker build --build-arg SOME_ARG=value build_config_folder

# or 

cd build_config_folder
docker build --build-arg SOME_ARG=value .

It needed to be

docker build --build-arg SOME_ARG=value -f build_config_folder/Dockerfile .

This is because the final path positional argument determines where the COPY command will look for files and folders.

Hope that helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM