My spring boot application was running fine but when I dockerized it I got File not Found error - spring

I don't know what I'm getting this error when I dockerized my spring-boot application
this is my Dockerfile
enter image description here

First, I wonder is your application working on your IDE?
Second, I think you make sure to build and package.
It is not sure to exists a jar file in your target folder.
You always have to build and check by yourself.
Make the build process automatic.
How about using this Dockerfile?
FROM maven:3.8.6-openjdk-18-slim as MAVEN_BUILD
WORKDIR /build
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package -Dmaven.test.skip=true
FROM openjdk:18-alpine
WORKDIR /app
ARG JAR_FILE=*.jar
COPY --from=MAVEN_BUILD /build/target/${JAR_FILE} ./app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
If maven or openjdk version not matched, check this site.
https://hub.docker.com/_/maven
https://hub.docker.com/_/openjdk

In your Spring Boot application, from what I can see your BookingController is missing. Make sure you have the file present and build your image accordingly.
For your Dockerfile, try to change add to ADD, as it seems your target files are not been copied into the image built.

Related

Should I build my gradle project using the Dockerfile

I'm reading about dockerization of Spring Boot applications and all (or almost all) tutorials are based on some simple Dockerfile like
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
It usually work (however you may need to change some build target paths) but as far as I understand it requires us to build the application jar before the docker build command will be run.
I'm actually working with Gradle but for the Maven, I guess, it looks the same.
My question is: is it good convention?
If I'll download some repostitory and run docker build, regardless having proper Dockerfile, it will fail because there is no target/*.jar file (at least if someone did not commit the /build directory :P).
Should we then include some ./gradlew build commands in the Dockerfile?
https://github.com/palantir/gradle-docker
you should use this project
or
jar_path=$(find . |grep $APP_NAME|grep jar|grep -v original|grep -v repository|grep -v templates)
mv $jar_path ./app.jar

Creating Docker image with springboot layertools - does not find profile specific application properties

I am following the tutorial here and here on how to create a layered Docker image from my springboot backend. I end up with the following Dockerfile:
FROM openjdk:8-jre-slim as builder
WORKDIR application
ARG JAR_FILE=target/*-exec.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM openjdk:8-jre-slim
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
The problem is that when I run this in my docker-compose with a Spring profile TST, that it does not find application-tst.properties. I can see from the logs that Spring profile TST is active on startup, yet it only loads the properties from application.properties.
As a sanity check I copied the properties from application-tst.properties over to application.properties and rebuilt my image, which then worked fine (it connects to the database container etc).
I extracted the contents of my executable jar (which is the jar from line 3 in the Dockerfile) and can confirm that application-tst.properties is present there. Anyone know what the issue is?
I found the answer and it's a really dumb one... The Spring profile is case sensitive, so
SPRING_PROFILES_ACTIVE=TST
should become
SPRING_PROFILES_ACTIVE=tst

Multi-Stage Spring Boot Dockerization Using Maven

Trying to get the multi-stage Spring Boot application Dockerfile that works.
The idea is to:
Build and package the project using mvn package command
Run the built .jar file
After some research, I found this article. It provides complete Dockerfile, but it does not work for me.
I modified the initial Dockerfile, and now it looks like this:
FROM maven:3.6.2-jdk-8-slim AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn -Dmaven.test.skip=true package -Ptest # This line does not work properly
FROM openjdk:8-jre
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/platform-0.0.1.jar /app/
ENTRYPOINT ["java", "-jar", "platform-0.0.1.jar"]
I created a docker-compose.yml that tries to build this Dockerfile:
[...]
api:
build:
context: .
dockerfile: Dockerfile
depends_on:
- mysql-db
ports:
- "8080:8085"
[...]
After running the docker-compose up --build -d command, I always get this error:
Am I missing something?
Running the mvn -Dmaven.test.skip=true package -Ptest command in the actual project folder works normally...
The problem was with my docker client installation. It somehow did not setup necessary permissions after installation. On other machines, this Dockerfile works fine.
Well, this was a lesson for some future times.

Dockerfile to create image of spring boot

I want to create Docker image for a spring boot application.It uses gardle as build tool.
In bitbucket repository i could see below files are placed
--> src
--> build.gradle
--> Dockerfile
--> gradlew
--> gradlew.bat
Now Dockerfile has below content
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
EXPOSE 8080
ENTRYPOINT ["java","-cp","app:app/lib/*","eds.billnq"]
When i try to create image using Dockerfile i get error
target/dependency/BOOT-INF library not found
target/dependency/META-INF library not found
My query is why here there is no gradle build steps in docker file ?
Also where target/dependency/ will be created ?
why here there is no gradle build steps in docker file ?
It looks like the Dockerfile is expecting the application to have already been built (by Gradle) before the Docker image is built, and for the resulting class files and jars to be available in various directories under target/dependency.
The Spring Boot Docker guide referred to by #WonChul Heo in the comments goes into how to build a Docker image using Gradle and specifically with the plugin gradle.plugin.com.palantir.gradle.docker:gradle-docker.
Also where target/dependency/ will be created ?
I'm guessing they should be created when you run your Gradle build, but without knowing more about how the build is configured, it's hard to say for sure.
I suggest you read through the guide again and compare what it recommends to what you have in your codebase, especially the Gradle build definitions.
At the end of the day, if you've pulled code from a private code repo and can't figure out how to make it build, it's probably best to seek out advice from the people who've committed to the codebase than from SO.
When working With Gradle ur jar file is going to be generated inside build/libs/{Your jar}
Can you please change ARG DEPENDENCY=target/dependency to ARG DEPENDENCY=build/libs and try? It will work.

How to natively build a servlet based Quarkus application using a web.xml as the deployment descriptor?

I'm setting up a simple servlet application using Quarkus. Where should I place the web.xml file and how should I deploy the application using the native build feature of Quarkus?
I have tried placing web.xml in project-name/src/main/resources/WEB-INF folder and natively built it using GraalVM docker image, but the built is not working. Dockerfile I used for the build is as of below.
Stage 1 : build with maven builder image with native capabilities
FROM quay.io/quarkus/centos-quarkus-maven:19.1.1 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
USER root
RUN chown -R quarkus /usr/src/app
USER quarkus
RUN mvn -f /usr/src/app/pom.xml -Pnative clean package
Stage 2 : create the docker final image
FROM registry.access.redhat.com/ubi8/ubi-minimal
WORKDIR /work/
COPY --from=build /usr/src/app/target/*-runner /work/application
RUN chmod 775 /work
EXPOSE 8080
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
I expected the output to be "Welcome", but the actual output is "Not Found".
You can place web.xml file in the project-name/src/main/resources/META-INF directory to get it working.

Resources