Wednesday, April 1, 2020

Docker - Spring Boot Application in Java

This ties into the other Kubernetes post because the purpose of containerizing apps in this way is to then be able to run them in docker images which can be pushed locally and then they can be used in a variety of ways in cloud environments.

First, I installed Docker locally to be able to create application containers. I setup a Kubernetes and got an IP to connect everything to. Since this is a Spring Boot app I had to create a spring boot image in Java. To help with the templating to get started I used spring.io although other plug-ins can do the same trick as well. I then imported the image to my IDE, created a Dockerfile and renamed the snapshot file in 'pom.xml' and restarted everything. From here I then enabled the Open JDK image for the app with Java 8 specifically and added the jar to deploy into a Docker container from the target directly in the root directory which exposes my app to port 8085.

Here is my Java IDE where I am configuring my Docker Spring Boot Application.



Here is Spring Boot loading up in the terminal.


                                                               
Here I am first downloading the Open JDK from the docker hub. Once its in my local machine then the image is run. Now below I pull up the docker images to see the images created. 

Then expose the jar to 8085 and the machine to 8085

I expose port 8085 for the app running in the container and in the machine map the port to the container. Next I push the app to port 8085. From here I had to do some quick debugging and found that the embedded Tomcat server wanted to start at port 8080 so I need to add into the application.properties a resource file to tell the server to use port 8085. From here I did a quick rebuild of the jar and ran it again. Success! Not so fast. There is still more to configure but it's coming together.


More errors to debug, but now this is a good one. This shows that the app is running and that there just needs to be some more defining for the resources. Here it doesn't know how to map an /error to a 404 page because that has yet to be created at this point in the process.


Now going back to the very beginning there was a resource file where I had defined the following:

@RestController@RequestMapping("/rest/docker/hello")
public class HelloResource {



So, although the Spring Boot jar app is running at port 8085, the file path needs to be defined to see the resource. Now if I navigate to the port where the app is running and specify the mapping to the exact folder where my contents are at 'localhost:8085/rest/docker/hello', I am greeted with the following:

Success! The Spring Boot Jar app is running locally in a docker container.
Now at this point it is ready to deploy and be pushed to the server. For Python or PHP it's just a matter of adding in different images for each language. Within the Dockerfile I give it the HTTP redirection for Tomcat in this case. Here I used Open JDK but an import can directly be done to the server. However, for a jar app Open JDK is the best. And since it is a spring boot application it already had the embedded tomcat server that's why I was able to expose the port and create an entry point. This is how you control the docker file. So in summary I downloaded the docker engine, over that I created containers. Then every docker image requires a Dockerfile to build the image so within that I then specified what images and software to use. Docker creates images and then with docker running I am able to give the port redirection to run the image Docker creates as a partition and runs the app in that container.

No comments:

Post a Comment

Automated Exploitation of a Bluetooth vulnerability that leads to 0-click code execution

This blog post covers an interesting vulnerability that was just discovered earlier this year and an open source free tool that was created ...