i just tried to build some microservices with spring boot. And wanted to deploy my eureka server on docker.
But i currently get the error:
PS D:2. reposervices> docker-compose build --no-cache
[+] Building 26.9s (14/14) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:11-jre-slim 0.5s
=> [internal] load metadata for docker.io/library/maven:3.8.4-openjdk-11-slim 0.6s
=> [build 1/6] FROM docker.io/library/maven:3.8.4-openjdk-11-slim@sha256:04f8e5ba4a6a74fb7f97940bc75ac7340520728d2fb051ec 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 808B 0.0s
=> [stage-1 1/3] FROM docker.io/library/openjdk:11-jre-slim@sha256:93af7df2308c5141a751c4830e6b6c5717db102b3b31f012ea29d8 0.0s
=> CACHED [build 2/6] WORKDIR /app 0.0s
=> CACHED [stage-1 2/3] WORKDIR /app 0.0s
=> [build 3/6] COPY pom.xml . 0.1s
=> [build 4/6] RUN mvn dependency:go-offline 24.4s
=> [build 5/6] COPY eureka-server/src /app/src 0.1s
=> [build 6/6] RUN mvn package 1.5s
=> ERROR [stage-1 3/3] COPY --from=build /app/target/*.jar ./eureka-server.jar 0.0s
------
> [stage-1 3/3] COPY --from=build /app/target/*.jar ./eureka-server.jar:
------
failed to solve: lstat /var/lib/docker/tmp/buildkit-mount1619314829/app/target: no such file or directory
PS D:2. reposervices>
This is my structure:
- services
--> | eureka-server
- Dockerfile
- pom.xml
- pom.xml
- docker-compose.yml
This is my docker-compose:
version: "3"
services:
eureka-server:
image: "eureka-server"
container_name: "eureka-server"
build:
context: .
dockerfile: "./eureka-server/Dockerfile"
ports:
- "8761:8761"
And this is my dockerfile:
# Use a base image with Java and Maven installed
FROM maven:3.8.4-openjdk-11-slim AS build
# Set the working directory
WORKDIR /app
# Copy the POM file
COPY pom.xml .
# Download the project dependencies
RUN mvn dependency:go-offline
# Copy the source code
COPY eureka-server/src /app/src
# Build the project
RUN mvn package
# Create a new image with a smaller base image
FROM openjdk:11-jre-slim
# Set the working directory
WORKDIR /app
# Copy the built JAR file from the previous stage
COPY --from=build /app/target/*.jar ./eureka-server.jar
# Expose the port that the application will run on
EXPOSE 8761
# Command to run the application
CMD ["java", "-jar", "eureka-server.jar"]
Maybe someone can help my why this docker runs into the error of not finding the jar.
When i run mvn package in the cmd i get this output:
D:2. reposerviceseureka-server>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.portshare.services:eureka-server >----------------
[INFO] Building eureka-server 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.0:resources (default-resources) @ eureka-server ---
[INFO] Copying 2 resources
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ eureka-server ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:2. reposerviceseureka-servertargetclasses
[INFO]
[INFO] --- resources:3.3.0:testResources (default-testResources) @ eureka-server ---
[INFO] Copying 0 resource
[INFO]
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ eureka-server ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- surefire:3.0.0:test (default-test) @ eureka-server ---
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ eureka-server ---
[INFO] Building jar: D:2. reposerviceseureka-servertargeteureka-server-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.023 s
[INFO] Finished at: 2023-08-08T21:43:29+02:00
[INFO] ------------------------------------------------------------------------
D:2. reposerviceseureka-server>
2
Answers
Change the COPY directive in your dockerfile to
COPY –from=build target/*.jar ./eureka-server.jar
The problem seems to be the directory of
jar
file is wrong, or not exist. I have encountered this type of error for many times, you can try to add some statements that output the directory structure of your project in the container, make sure that the/target
folder in your project is exists.Another possibility for this problem is that wildcards (*) are not supported in the path. You can try to use the sepcific name of the
jar
file with varbiles. For example:MyProject-SNAPSHOT-${VERSION}
, the pattern of filename can be setted in thepom.xml
file.Since I’ve had this problem before, these are the two approaches I’ve tried before.