MetaCoder
MetaCoder

Reputation: 478

Unable to run Spring Boot app with Multi-Stage Docker Build

I've been following the guide from the spring website [here][1] but when I try and run Docker I get a class not found exception. I cant see what I'm doing wrong here.

Below is my dockerfile:

# syntax=docker/dockerfile:experimental
FROM openjdk:15-jdk-alpine AS build
WORKDIR /workspace/app

COPY . /workspace/app
RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build
RUN mkdir -p build/dependency && (cd build/dependency; jar -xf ../libs/*.jar)

FROM openjdk:15-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=/workspace/app/build/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","TFSSearchServiceApplication"]

And If I look into the docker image here's the folder structure I see:

/app # ls
META-INF                           application-local.yml              application-production.yml         application.yml                    network
application-local.properties       application-production.properties  application.properties             lib
/app/network/mytest/search # ls
TFSSearchServiceApplication.class  controller                         exception                          service
config                             converter                          model                              validators
/app/network/mytest/search # 

Which is what I'd expect, however can anyone spot why I'm getting the class not found error, I can see it from last codeblock.

Many thanks [1]: https://spring.io/guides/topicals/spring-boot-docker/

Upvotes: 0

Views: 937

Answers (1)

Manuel Brnjic
Manuel Brnjic

Reputation: 726

change your Dockerfile to the following (only change in the last line):

syntax=docker/dockerfile:experimental
FROM openjdk:15-jdk-alpine AS build
WORKDIR /workspace/app

COPY . /workspace/app
RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build
RUN mkdir -p build/dependency && (cd build/dependency; jar -xf ../libs/*.jar)

FROM openjdk:15-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=/workspace/app/build/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","network.mytest.search.TFSSearchServiceApplication"]

The reason for the ClassNotFoundException is that your classpath (-cp) is searching inside /app and /app/lib/*. Your SpringBootApplication resides in a package (network.mytest.search.TFSSearchServiceApplication). So you have to point the java command to it by specifying the package additionally.

Make sure TFSSearchServiceApplication has the @SpringBootApplication annotation.

Upvotes: 1

Related Questions