How to I externalize my configuration for my spring-boot war application? - spring-boot

I have a spring-boot war application that is packaged using Maven, and I need to deploy it as docker image. Now I am done with creating docker image, my current problem is how do I pass external configuration? I am not running my spring-boot application as java -jar myapp.war. The person responsible for deploying this image will need to pass the path of the configuration file to docker image.

Related

Start spring boot jar file as part of tomcat start

There is a way I can start the spring boot jar file as part of tomcat start(catalina.bat start).
I have a spring cloud api gateway which does not support war file deployment.
In a bare metal box, I have installed the tomcat, and as part of deployment I have converted all microservice into war file and able to deploy it into tomcat/webapp folder and up and running.
As spring cloud api gateway does not support war file, I kept this application with embed tomcat.
Now I need this jar file as well get started along with the tomcat [catalina.bat start].
In tomcat I have seen there is something shared.loader option in catalina.properties file.
I have kept the jar file in below path as shown below and when I try to start it, jar file is not getting picked up.
Need to know if there is a way to start the spring boot application [jar file] as part of tomcat [catalina.bat start] start.

How do I override configuration in application.yml in spring boot (2.4.2) generated docker image?

I used spring boot 2.4.2
Generated docker image using the command mvn spring-boot:build-image -Dspring-boot.build-image.imageName=XXXXXX.azurecr.io/fake-XXXXX:0215
I have hostnames in application.yml that I need to override
So I passed -Dspring.data.mongodb.host=mongo_db in docker-compose under command: BUT it is not overriding. .. Below screenshot for reference.
I understand that Buildpacks are used to build the layered jar.
If this was docker image created by me manually and not by spring boot then I would have ENTRYPOINT in Dockerfile and command in docker-compose where I would have given command: Dspring.data.mongodb.host=mongo_db -jar fake-XXXX.jar
I know #6 works but would like to use the latest and greatest spring boot in my new project. Hence kindly let me know your thoughts.
Why don't you try the approach via Spring Config or Harshi Corp Consul KV feature and read your config from there. Check-in your config to a repo and have config framework read it from there and pass it to your app? You can change the config at runtime (probably requires restart of containers to reload the modified KV fields in some case)

How to load the web app folder to docker container

I am new to the docker technology. I have a spring boot application running on my local machine. I need to move the target jar file to the docker container.
This spring boot application is contains rest services and some controller will display the view (JSP) with the help of dispatcher servlet.
While creating the container, I am getting 404 Error for the views returned from the dispatcher servlet. What is the command I need to place in my dockerfile to move my static web app folder to the container ?.
Thanks in advance.
Container is working fine for the rest services. Rest services to perform CRUD with mysql server.
My Dockerfile :
FROM openjdk:8
EXPOSE 7070
ADD target/docker_app.jar docker_app.jar
ENTRYPOINT ["java","-jar","docker_app.jar"]
You should not wrap the the web application as jar.
add the following tag in your pom xml
<packaging>war</packaging>
change the Dockerfile command to deploy war file instead of jar.
Dockerfile :
FROM openjdk:8
EXPOSE 7070
ADD target/docker_app.war docker_app.war
ENTRYPOINT ["java","-jar","docker_app.war"]

How to manage custom common libraries built in maven for my Application in kubernetes

I need to understand how to deal with common libraries that i have created which my Application depends upon. When i create jar for this app using maven it creates a package. But how do we maintain or configure other common libraries which are listed in pom.xml of this application?
should we have maven also as an image in the docker file?
Please explain in detail.
My current progress is explained below: I have an Application A which has other dependencies like B and C libraries which i have specified in pom.xml. When i run application A in my local system it uses local repository that i have configured in user settings for maven. SO it works fine.
So how do we maintain this in kubenetes.
The images that you use to build the containers that run on Kubernetes should contain everything that's needed to run the application.
When you create the JAR file for your application, this JAR should contain the dependencies. There are different ways to achieve this, using both Maven or Gradle. This is an example using Maven and its Apache Maven Assembly Plugin. This is another good user guide on how to achieve it.
Then, you need to create a container image that can run that JAR file, something like
FROM openjdk:8-jdk-alpine
EXPOSE 8080
WORKDIR /opt/app
CMD ["java", "-jar", "app.jar"]
COPY build/libs/app.jar /opt/app
Once this container image is published on a registry, whenever Kubernetes needs to schedule and create a container, it will just use that image: there is no need to re-compile the application and its dependencies.

Docker | How to build and deploy war file in jboss container hosted in Docker

I have a jboss image hosted in Docker, along with several others. I am able to run the jboss image and use it as container to deploy webapps.
Currently using IntelliJ to configure a Docker configuration and deploy war files directly from IntelliJ and pointing to the docker configuration within IntelliJ.
I am looking for ways by which I can deploy this war file directly in my jboss image at Docker.
Basically looking at ways to deploy war file without any IntelliJ intervention, with the use of docker-compose to build jboss image along with added war targets successfully deployed.
What sort of changes need to be done in jboss.yml file and Dockerfile for jboss image?
If you want the .war file to be integral part of your image, then you just need to add it as a file resource to your jboss deployment dir during your image assembly via dockerfile. Say your docker file goes like this:
FROM jboss/wildfly
COPY myapp.war /opt/jboss/wildfly/standalone/deployments/
Of course you need to adjust the paths to match your setup and distribution, you can for example use maven docker plugin.
Other option is just to build your server without any deployments and use jboss cli or web admin interface to deploy it. Again you can automate it via maven or RUN command in dockerfile.

Resources