How to write dockerfile for front end web app - java

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.

Related

Not able to access the docker containerised java web app on exposed port

I have Java web app in docker container.
Now container is exposed on host's port 8080 from container's port 8080 by running docker run command.
docker run -d -p 8080:8080 --name myTomcat -v $(pwd)/out/artifacts/DockerJavaWebAppWarExploded:/usr/local/tomcat/webapps/ tomcat:latest
There is no other process running on 8080 port ,but i am not able to access the application from browser http://localhost:8080/.
Container logs :
Please help.
It would be appreciated.
Check if http://0.0.0.0:8080 returns values. If yes, you may add an enrty for 0.0.0.0 in your hosts file.

Spring boot jar docker deployment throwing Connection Exception with MySQL Container

I just started exploring docker today and I've been trying to spin-up Spring Boot Web app backed by MySQL DB.
I pulled a MySQL container and ran it using:
docker run -t --name mysql-docker-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=**** -e MYSQL_DATABASE=spring_app_db -e MYSQL_USER=app_user -e MYSQL_PASSWORD=**** -d mysql
My application.properties file:
spring.datasource.url=jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.username=app_user
spring.datasource.password=test123
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
with this, when I run my spring boot in IDE it's able to connect and perform CRUD on db table.
However, when I try to deploy it on docker and link with MySQL container it throws Connection Refused error.
My Dockerfile:
FROM openjdk:11
LABEL maintainer="baljinder#gmail.com"
VOLUME /tmp
EXPOSE 8080
ADD target/demo-0.0.1-SNAPSHOT.jar bootmysql.jar
ENTRYPOINT ["java", "-jar", "bootmysql.jar"]
The command I'm using to run docker image of the boot:
docker run -t --name spring-jpa-container -p 8080:8080 --link mysql-docker-container:mysql spring-jpa-app
Can someone please help with this. I've tried deploying both on the same container network by creating a docker network (docker network create -d bridge sql_spring_network) but the error persists.
When using the legacy linking of containers using the --link flag. The "linked" container is available as a host in the running container with it's container name. So in your case, the mysql container is available in your app container with hostname mysql.
Your database url, therefore should be: jdbc:mysql://mysql:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
use:
jdbc:mysql://mysql-docker-container:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
replace:
jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10

how to access mysql docker container in spring boot docker container

i have 2 docker file
1. mysql-dockerfile
FROM mysql:5.5
EXPOSE 3306
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE ToDoList
command used to build dockerfiles are as below
sudo docker build -t mysql-img -f mysql-dockerfile .
sudo docker run -d --name mysqlcontainer -p 3030:3306 mysql-img
2. java-dockerfile
FROM openjdk:8-jre-alpine
EXPOSE 9090
WORKDIR /usr/src
COPY target/*.war todoApp.war
CMD ["java","-jar","todoApp.war"]
command used to build dockerfiles are as below
sudo docker build -t java-img -f java-dockerfile .
docker run --name javacontainer -d -p 4040:9090 java-img
spring boot application consist jdbc url as follow
spring.datasource.url=jdbc:mysql://localhost:3030/ToDoList
i am not able to start project because spring boot application in docker is not able to connect mysql db which is in another container.
one solution i found is to bring two docker container in one docker network or link docker container.
can anyone please suggest good solution, modified docker run command and modified jdbc url.
Put them into one network and use container names as hostnames:
docker network create foo
docker run --network=foo --name mysqlcontainer -d mysql-img
docker run --network=foo --name javacontainer -d java-img
Dont expose ports - they are exposed automatically between containers inside network.
To connect inside, use mysqlcontainer:3306 and javacontainer:9090.
To connect from host, you will need port exposing.

Docker springboot app update war file without rebuilding using maven

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?

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