Spring Boot + Docker + Bit Bucket + Automate Deployment

An idea behind this blog to walk through the auto build of docker image in docker hub for Spring boot application.
Note: – This method is not production level, so use it for staging or development phase only.
(Why it is not suitable for production ?)
[As we have to push the jar to the git repository, to reduce few steps.]
Technology Stack Used
  • Spring Boot [1.4.1 Release]
  • Java 8
  • Maven 2
  • Bit Bucket
  • Docker Hub
Prerequisite
  • Docker Hub Account
  • Bit Bucket Account
  • Minimum knowledge of Spring boot and Java
  • Link Bit Bucket to Docker Hub. CLICK HERE for the reference.
Steps
  1. Create Spring boot Application Capable of creating docker image
    • Create simple spring boot application with web module.[CLICK HERE to create using spring start.io or get it from BIT-BUCKET repo (master branch)].
    • Add Simple test controller to the Spring Project or check out to the branch controller-test.
    • Add Docker support to your current spring boot application or check out to branch docker-support. [To learn how to add docker support to spring boot application you check this blog.]
  2.  Create New Repository and link for auto creation of image
    1. Go to docker hub and click on create -> create automatic build
    2. Click on create Auto-build bit-bucket
    3. Select the desired bitbucket repository
    4. Fill required information and click on create.
    5. Go to build setting and provide the branch name from which image should be created and save changes.
    6. Click on build details, it will be empty right now
    7. Make Some changes in that particular branch and push it to the Origin.
    8. Now again check the build details tab, there will be new entry populated with status queued.
    9. Wait for some time its status will change to building than to success.
    10. Now your docker image is ready to use.
  3. Test created docker image
      1. Now pull the docker image
      2. Run the docker image with command [docker run –name docker.test -d -p 8088:8080 jainamit333/docker.demo:docker-support]
      3. Navigate to http://localhost:8088/docker/test.It will show the desired result.
Now each time user push to anything to branch, new docker image will be created.

Dockerize Spring Boot Application

This blog will help the audience to enable them to create docker image from a Spring boot Application.We have created a Spring Boot with maven.

We have already created the code repository. This repository contain 3 branches

1.Master: – Vanilla Spring Boot with Web Module

2.Controller-Test: – Master with test Rest repository.

3.Docker-Support :- Added docker support to Controller-Test branch

Prerequisite

  • Spring Boot Knowledge
  • How to create simple DockerFile

Here we will see what changes are required to add docker support to Spring Boot Application. It is the changes required to create Docker-Support branch from Controller-Test branch.

Changes Required in Pom

We need to add below mention plugin in the pom.

<plugin>
   <groupId>com.spotify</groupId>
   <artifactId>docker-maven-plugin</artifactId>
   <version>0.2.3</version>
   <configuration>
      <imageName>horizon/${project.artifactId}</imageName>
      <dockerDirectory>/</dockerDirectory>
      <resources>
         <resource>
            <targetPath>/</targetPath>
            <directory>/</directory>
            <include>${project.build.finalName}.jar</include>
         </resource>
      </resources>
   </configuration>
</plugin>

Add DockerFile in the project path

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD target/docker-0.0.1.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8080

There are two ways to create a docker image from the current source now.

1.Create docker image using maven plugin [can also be used using Jenkin worklow]

Run the command from project directory:- mvn clean package docker:build

2.Create docker image using docker build command

Run mvn install:- mvn clean install

Create docker image by executing the following command from project directory:

docker build  -t  docker.demo:latest  .

Rotate the matrix by 90 degree in java

We are rotating the content of a matrix by 90 degree.

Assumptions

a is the given matrix.

We will just print it in the rotated form.

///code

int a[][] = {{1,2,3,4,5,6},{7,8,9,10,11,12}};

//no. of column
int m = a[0].length;

//no. of rows
int n = a.length;

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
for (int i = 0; i < m; i++) {
for (int j = n-1; j >=0; j–) {
System.out.print(a[j][i]+” .. “);
}
System.out.println();
}

How to check if string can be used to create a palindrome

This is a code to check if we can make a palindrome using all letters of string

Input: str is a given string.

NOTE: assumption every character have to be use present in the string, and only once.

Logic: only one alphabet can have odd occurence,rest should have even occurence.

 

ALGO:

 

public static void main(String asd[]){

 

int count= 0;

int odd_occurence = 0;

//str is given string.you can initialize str with ant input you want.

String str =  “apapqqqqjjk”;

Boolean output = true;

for(int i =0;i<str.length();i++){

                for(int j=0;j 1)

                {

                                output = false;

                                break;

                }

}

 

if(output){

                system.out.println(“palindrome can be created”);

}else{

                system.out.println(“palindrome cannot be created”);

}

 

}

To search in binary tree

Following function help to search element in binary tree
Pre Requisite: Java ,Binary tree.
Level: medium
It is a simple method which will return the Search result in binary tree.
We have followed the recursive approach to solve this problem.
Binary tree is tree , where each node have at most 2 child.

As the parameter ,we will pass the Object which is the root of the binary tree.
“searchVariable” is the value which we have to search
METHOD:

public boolean searchInBinaryTree(Node N){

boolean result = false;
if(N == null)
return false;

if(N.value == searchVariable)
return true;

if(N.left != null)
result = searchInBinaryTree(N.left);

if(N.right != null)
result = searchInBinaryTree(N.right);

return result;
}
Note : link below will give you more information and help you in understanding in better way.

Remove extra spaces from string in java

This is a code to remove extra spaces from string.
String str = “i  am     reading    this book  since yesterday.”;

String s1 = “”;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ' && str.charAt(i + 1) == ' ') {
str = str.substring(0, i).concat(
str.substring(i + 1, str.length()));
i–;

}

}
System.out.println("string without space :"+str);

Posted from WordPress for Android

Count number of words in java.

Following code will help to count the number of words in given string.

String str = “i  am     reading    this book  since yesterday.”, space = ” “;
/* int spaces = 0;

for (int i = 0; i < str.length(); i++) {

if (str.charAt(i) == ' ' && str.charAt(i + 1) != ' '
&& str.charAt(i + 1) != '.') {

spaces++;
}

}
spaces++;
System.out.println("No. Of words =" + spaces);

Posted from WordPress for Android