I have a spring boot 3 application that I am trying to deploy on a docker container on an M1 macbook. I am getting error when trying to run the container:
docker run -p 8080:8080 myapp_backend:0.0.1
Error: Unable to access jarfile /app.jar
Dockerfile:
FROM maven:3.8.4-openjdk-17 AS build-stage
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY ./src ./src
RUN mvn clean install -Dmaven.test.skip=true
# Stage 2
FROM openjdk:17-jdk-slim AS production-stage
WORKDIR /app
COPY --from=build-stage /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
Docker build logs:
> docker build -t myapp_backend:0.0.1 .
[+] Building 3.2s (15/15) FINISHED docker:rancher-desktop
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 735B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:17-jdk-slim 3.2s
=> [internal] load metadata for docker.io/library/maven:3.8.4-openjdk-17 3.2s
=> [build-stage 1/6] FROM docker.io/library/maven:3.8.4-openjdk-17@sha256:7106ce47b5c226ee21bac386cb706ff9f3db7ac2f15e251e3aa491fcbfc30028 0.0s
=> [production-stage 1/3] FROM docker.io/library/openjdk:17-jdk-slim@sha256:aaa3b3cb27e3e520b8f116863d0580c438ed55ecfa0bc126b41f68c3f62f9774 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.64kB 0.0s
=> CACHED [production-stage 2/3] WORKDIR /app 0.0s
=> CACHED [build-stage 2/6] WORKDIR /app 0.0s
=> CACHED [build-stage 3/6] COPY pom.xml . 0.0s
=> CACHED [build-stage 4/6] RUN mvn dependency:go-offline 0.0s
=> CACHED [build-stage 5/6] COPY ./src ./src 0.0s
=> CACHED [build-stage 6/6] RUN mvn clean install -Dmaven.test.skip=true 0.0s
=> CACHED [production-stage 3/3] COPY --from=build-stage /app/target/*.jar app.jar 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:f5ca156d9ebda2118100dc1d75ec6240f3e9c283686ea51599cad313bf5744c8 0.0s
=> => naming to docker.io/library/myapp_backend:0.0.1 0.0s
Based on this:
WORKDIR /app
COPY --from=build-stage /app/target/*.jar app.jar
your app.jar goes to /app/app.jar
, not /app.jar
. The current directory in docker image build is always the latest WORKDIR
.
Yet in your entrypoint you have this:
ENTRYPOINT ["java", "-jar", "/app.jar"]
where you try to run the app.jar
from the root (/
) directory.
You have two good options. Either
app.jar
to the correct place under root: COPY --from=build-stage /app/target/*.jar /app.jar
or
app.jar
from the correct place under /app/app.jar
:
ENTRYPOINT ["java", "-jar", "/app/app.jar"]