Sunday, November 3, 2019

How to create Docker Image and push java app in a Docker Engine

In this blog, I am going to share my knowledge on the creation of a docker image and how can we run in a Docker Engine.

Prerequisite

  • Basic Knowledge of Docker
  • Docker must be running on your machine.
  • Good to aware of Spring boot application.
I already have one spring boot application in my IntelliJ which expose one endpoint /users/{id}. We will see how can we push and run this application in a docker container. 



We need to create one file named Dockerfile to add docker instruction (Check above image).


Now go to Terminal and check whether the docker is running or not on your machine.


Run docker build to create an image and push it to the container using the command.
docker build -f Dockerfile -t docker-spring-ehcache .

The above command will execute all the operations that we have mentioned in our Dockerfile like pulling OpenJDK 8 from the docker hub if not exist.

Let's see if our image got pushed to docker containers or not by listing all docker images.
docker images


Great! Our image is present in the docker container. Let's run it.
docker run -p 8070:8085 docker-spring-ehcache

Over here, we are telling the Docker to start the application and map docker container port 8085 to our local port 8070. 

Note: Make sure your application has started at 8085 in docker container else it won't be able to map it. Spring boot by default start application on port 8080 so please specify server.port-8050 in application.properties file.



Now go to your browser and hit the endpoint and see if we are getting the response from localhost:8070 or not.



Happy Coding!!!

Friday, November 1, 2019

Sonar Integration with Maven

In my previous blog, we have already seen how to setup SonarQube server on Windows 10. We have also seen that how can we generate sonar report using sonar-scanner. In this blog, I’ll show you how to generate sonar report by configuring sonar dependency to maven project. 

Steps to setup sonar in Maven

  • We have to configure pluginManagement and Profile for Sonar in pom.xml file
  • Add below pluginManagement dependency to your pom.xml 
<pluginManagement>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

</plugin>

<plugin>

<groupId>org.sonarsource.scanner.maven</groupId>

<artifactId>sonar-maven-plugin</artifactId>

<version>3.6.0.1398</version>

</plugin>

<plugin>

<groupId>org.jacoco</groupId>

<artifactId>jacoco-maven-plugin</artifactId>

<version>0.8.4</version>

</plugin>

</plugins>

</pluginManagement>


  • Add Profile to pom.xml file

<profiles>

<profile>

<id>coverage</id>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

<build>

<plugins>

<plugin>

<groupId>org.jacoco</groupId>

<artifactId>jacoco-maven-plugin</artifactId>

<executions>

<execution>

<id>prepare-agent</id>

<goals>

<goal>prepare-agent</goal>

</goals>

</execution>

<execution>

<id>report</id>

<goals>

<goal>report</goal>

</goals>

</execution>

</executions>

</plugin>

</plugins>

</build>

</profile>

</profiles>


  • Build the project, execute all the tests and analyze the project with SonarQube Scanner for Maven:
                 mvn clean verify sonar:sonar

  • Once done, Check your SonarQube which will generate the code analysis report for your current project. My project name was api-gateway so it generated with the name api-gateway.


Code Analysis using SonarScanner on Windows 10

In my previous blog, we have already seen how to setup SonarQube server on Windows 10. In this blog, I’ll show you how to generate sonar report using SonarScanner. 

Step to setup SonarQube


  • Unzip it and open sonar-scanner.properties which are under conf directory.
  • Edit the below lines

  • Now, go to your project folder directory, open command prompt and run sonar-scanner.bat.

  • It will do the analysis and then post the result to the SonarQube server http://locathost:9000/ having the project name as sonar key that we have configured in sonar-scanner.properties file.


  • You can check the JUnit test code coverage as well by clicking on Coverage.

Happy Coding..!!!

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...