Docker springboot app update war file without rebuilding using maven - java

I am running Docker for Windows version 1.12.3 and have a spring boot application running in a Docker container.
I'm using a maven container to build my application.
docker run --rm -it -v $(cd)\trunk:/project maven:3.3.9-jdk-8 mvn clean package -f /project
Which works fine it builds my war inside truck, then I run a tomcat container. copy my war file into the container and my application runs on port 8080.
docker run -it -p 8080:8080 -p 8005:8005 -p8009:8009 -v $(cd)\trunk\target\myAPP.war:/usr/local/tomcat/webapps/ROOT.war tomcat:8.0.38-jre8
Now if I make a change (small) in my code (like add a comment, or return hello "just an example") the only way I can see the change in my application is to run the maven container again (which takes about 5 mins or so) and then run the tomcat container.
Is there a way for me to just update the code (like the way Spring Tool Suite does it) and then run the tomcat container so it picks up the changes in my code?

Related

Docker build does not create an image but the application starts anyway

Currently I am running windows with docker for windows installed.
I have made a java application in Spring Boot that I want to build an image of and run in a docker container.
What am I doing wrong?
When I run the bellow and can see in my cmd prompt that the application starts and run. But there is no image in in docker and nothing running there.
#
# Build stage
#
FROM maven:3.8.4-openjdk-17 AS build
COPY src /test/src
COPY pom.xml /test
#RUN mvn -f /test/pom.xml clean package
RUN mvn -f /test/pom.xml clean package
#
# Package stage
#
FROM openjdk:17-alpine
COPY --from=build /test/target/test-0.0.1-SNAPSHOT.jar /usr/local/lib/test.jar
ENTRYPOINT ["java","-jar","/usr/local/lib/test.jar"]
What am I missing?
I have the application in a folder /test and the Dockerfile is also under /test. I go to this location in the cmd prompt and enter:
docker build -t testapp .
The command you are running, docker build -t testapp ., only creates the docker image that you can check with the command someone stated in the comments : docker images -a.
To run the image in a docker container you must use the run command : docker run testapp. Then you will see the container in the Docker app.
docker build will only build docker image
if you want to run the image in your docker try running the command
docker run testapp
docker build builds a new image from the source code.
docker create creates a writeable container from the image and prepares it for running.
docker run creates the container (same as docker create) and runs it.
I was doing everything correct except for that when I thought the image was ready/built I run test of my java application that looked as the application had started.
By setting <maven.test.skip>true</maven.test.skip> I skipped the test and the build was finished. I got the image which I now can start. Thank you all for your question and assistance.

How to develop with Spring Boot in a Docker container and any IDE

I use docker-compose to launch different Spring Boot apps.
My docker images are defined with this kind of Dockerfile:
FROM openjdk:8-jdk-alpine
ADD app.jar app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
However, I would like to benefit from debugging and hot-reload features using something like mvn spring-boot:run without being dependent of a particular IDE.
What is the best way to accomplish debugging and hot-reloading with Spring Boot in a Docker container without being dependent of a particular IDE?
Notes:
my source files are build into a jar (with Maven) which is copied to a different location containing the definition of my Docker images ; meaning my sources files are not in the docker image.
the reason I want to develop in the Docker container is that my apps depend on each other, and are configured in the docker-compose environment, so I cannot easily run one app alone outside the docker network and environment.
I thought of mounting a volume containing my spring boot projects in the docker containers, and then use mvn spring-boot:run in the container ; but I can't prevent maven to download all dependencies from the internet (I tried specifying a local repository containing all my dependencies without success). I would like to know if this a decent solution and how to make it work.
You have to follow the following steps to build and run spring boot application in docker.
Step-1 : Create a File called Dockerfile in your Project.
Step-2 : Write the Following Code on you Dockerfile
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.6-jdk-11 as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build a release artifact.
RUN mvn package -DskipTests
# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim
# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/your-app-name*.jar /your-app-name.jar
# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/your-app-name.jar"]
Step-3 : Start Your Docker Desktop Application
Step-4 : Open Your Terminal or Windows PowerShell. Then go to Project Directory.
Step-6 : Write the Following Command to create image for your application (You must have internet connection to download all dependencies).
docker build -f Dockerfile -t your-app-name .
Step-7 : After image creation success. Write the following code to run the image in Docker container.
docker run -p docker-port:app-port image-name
Following your line of thinking you can try to copy your dependencies from a volume into the project container and then use the offline mode in something like this:
FROM openjdk:8-jdk-alpine
WORKDIR /app
# copy the Project Object Model file
COPY ./pom.xml ./pom.xml
# copy your dependencies
COPY app.jar app.jar
# copy your other files
COPY ./src ./src
# Set fetch mode to offline to avoid downloading them from the internet
RUN mvn dependency:go-offline
Apparently it's also possible to configure the offline mode globally by setting the offline property in the ~/.m2/settings.xml file, you can setup that and copy your m2 file and reference it when running the container
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<offline>true</offline>
</settings>
mvn -Dmaven.repo.local=~/.m2/settings.xml ...
You can find more information here:
https://www.baeldung.com/maven-offline
Specifying Maven's local repository location as a CLI parameter

How to write dockerfile for front end web app

Trying to deploy my first Java web app to Google Kubernetes. It must have 3 containers.
1 - front end web app
2 - back end Java web app- Jersey web service
3 - Postgres server
The whole web app is working on Eclipse Jee (Tomcat) on my laptop with no issue. The web app is very simple SPA with no Maven or Gradle build.
For backend: Dockerfile
FROM tomcat:9.0
ADD backend.war /usr/local/tomcat/webapps/backend.war
EXPOSE 8080
The image from the above is working fine. But for front end web app, I am really confused. Tried following variants without any success:
a)
FROM tomcat:9.0
ADD frontend.war /usr/local/tomcat/webapps/frontend.war
b)
FROM tomcat:9.0
COPY frontend.war /usr/local/tomcat/webapps/frontend.war
EXPOSE 8080
c)
FROM 8.0-jre8-alpine
COPY frontend.war /usr/local/tomcat/webapps/frontend.war
When I tried to access my site using the loadbalancer IP which google provided on browser, getting Not reachable message.
Here is my sample web application with backend as mysql database.
Front-end Dockerfile
FROM tomcat:9.0
ADD art-gallery-management.war /usr/local/tomcat/webapps/art-gallery-management.war
WORKDIR /usr/local/tomcat/
CMD ["catalina.sh" "run"]
EXPOSE 8080/tcp
Back-end Dockerfile
FROM mysql:latest
WORKDIR /docker-entrypoint-initdb.d
ADD Schema.sql /docker-entrypoint-initdb.d
CMD ["mysqld"]
EXPOSE 3306/tcp
Starting containers
docker container run -d --name art-gallery-management-db -e MYSQL_ROOT_PASSWORD=vision -p 3306:3306 bukkasamudram/art-gallery-management:db-latest
docker container run -d --name art-gallery-management-app --link art-gallery-management-db -p 8090:8080 bukkasamudram/art-gallery-management:app-latest
Make sure to use link option to link front-end container with back-end container.

Deploy SpringBoot Microservices in Docker

I have a microService jar and docker file at my windows dextop x folder.
I have installed docker in my windows PC.
I have the below docker.server file
FROM openjdk:8-jre
ADD target/shopfront-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8010
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Now I want to build a docker image from this docker file, then what needs to be done.
I opened the docker terminal and paste the below command
docker build --file=Local location of Dockerfile.server
//(which is desktop x folder)\
--tag=microserviceName on my wish latest --rm=true
.
But it's not working.
As well as I get the current directory and I tried to copy the jar and the docker file in the current docker directory that is not happening.
Please guide what needs to be done.

Dockerizing Spring Application

I have a working app using Spring Framework + AngularJs front-end.
I do deploy it on amazon AWS before by simply packaging mvn package into .war file.
Now I need to setup a linux env in docker locally to debug some additional functionality (Using windows as the main OS) and preferably to deploy this docker container in future.
I do seen some articles on dockerizing the Spring Boot app.
This is the example dockerfile from spring.io
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
My question is - how do I run my .war file on docker container?
If you're looking to run this application on your local Linux machine, then you can create this Dockerfile in the same directory where the WAR file exists.
FROM tomcat:8.0.21-jre8
RUN [“rm”, “-rf”, “/usr/local/tomcat/webapps/ROOT”]
COPY dbconnect.war /usr/local/tomcat/webapps/ROOT.war
CMD [“catalina.sh”, “run”]
You can then build the Docker image and provide your custom tag:
docker build -t <your-username>/tomcat-example:latest .
Finally you can run this container.
docker run -p 8080:8080 -d --name tomcat <your-username>/tomcat-example:latest
You can check out these detailed examples here if need to run this application with a database or a web server.
https://github.com/dchqinc/dchq-docker-java-example
https://dzone.com/refcardz/java-containerization
You don't deploy a .war file in docker just like you deploy it into a tomcat server. You need to have a main(String args[]) entry point which is also specified in your jar's manifest file. Your app.jar specified as ENTRYPOINT will then run your main(). Inside the main() you can run an embedded web server which runs your application.
If you configure/package your Spring Boot app as an executable .jar instead of a .war (Spring Boot Initializr will do this for you if you select the .jar option), then running it in your container is a matter of adding an entrypoint to run java with -jar and your jar name, like
ENTRYPOINT ["java", "-jar", "/opt/yourapp-0.0.1-SNAPSHOT.jar"]

Categories

Resources