Docker Hub – Push and Pull Images – Deploy as Container with Example

Docker Hub – Push and Pull Images – Deploy as Container with Example

In our previous article, We learnt how to Dockerize SpringBoot Application with Simple Hello World example.  In this post, let us see how can we push the image to docker hub and how can we pull it and deploy the application as docker container.

So for pushing and pulling the image from Docker Hub, first we need to create an account in Docker hub.

Docker Hub – https://hub.docker.com/, We can login here and create an account.

We are going to use the same example used in our previous article,

 

 

pom.xml

AppController.java

App.java

Dockerfile

First let us create an image from the above docker file,

docker image build -t “sampleImage” .

In this example let us use image name as helloworld,

docker image build -t “helloworld” .

The above command will generate the image and can be seen with command,

docker images

We have successfully build the image, So in order to push the image to docker hub. First we need to tag it,

docker image tag “imageid” dockerhub username/repository

For this example, My dockerhub account is sriis1987,

docker image tag 09b9013b2982 sriis1987/helloworld

This will tag the image name, you can see it with docker images.

Then we need to push it,

docker image push dockerhubid/repository

For this example,

docker image push sriis1987/helloworld

Now in our docker hub account, we can see the image been successfully pushed.

 

When clicking this repository, we can see our pull command too (public view),

Now we can pull this image and we can deploy the container using imageid.

docker container run imageid.

Suppose if we update the same application, do we have to create new repository?

No, We dont have to.

Suppose if you update the existing file and you can push the image to same repository. It will just update the same image.

Example:

docker image tag “imageId” sriis1987/helloworld:v1

docker image push sriis1987/helloworld:v1

The above command will update the repository with the new image.

NOTE:

Suppose if it shows cannot authenticate or need to authenticate use command,

docker login

This is request for username and password of docker hub.

 

Dockerize SpringBoot Application – Hello World Example

Dockerize SpringBoot Application

In this post, Lets us create a simple SpringBoot application – Hello World and dockerize and deploy the application as container.

What is Docker?

Docker is a tool which helps us to create, deploy and run applications by means of Containers. Containers are like a package which has all the dependencies and libraries which is required to run the application successfully.

Docker is a platform for deploying Microservices.

There are many commands used in dockers, but for our example purpose we are going to use only 2 commands.

Commands to build an image and deploy as container.

Creating Image:

docker image build -t “imagename” .

Deploying Container:

docker container run “imageid” –> Generated after image creation.

Note: Docker has to be installed for the below application to work.

Project Structure:

We are going to create a sample controller and main springboot class.

pom.xml

AppController.java

App.java

Dockerfile:

So what are we doing with Dockerfile?

FROM – used to pull an image, here were taking the latest of Ubuntu. Usually the pull is from dockerhub (DockerHub). There are many base images already available in dockerhub. Similar to ubuntu, there is alphine, openjdk etc. which can be pulled from dockerhub using FROM.

MAINTAINER: It is for our own purpose, including the name

EXPOSE: Exposing the port where 8080:8080. First 8080 is internal port where as second is external exposure

WORKDIR: Working directory inside the container.

COPY: This is where we copy our jar file which is available in Target folder to within docker container. Here i am copying to /usr/local/bin/

ENTRYPOINT: This is where we run our application, we usually run as java -jar sample.jar the same is used in entry point.

After this, we run mvn clean install.

So, How to create image from Dockerfile?

Before creating an image, if you are using OracleVM make sure the instance is running.

We are going to use command prompt.

Command to build image: docker image build -t “helloworld” .

From the above the image has been created with our docker file.

Step 1: It has pulled openjdk:8 (FROM openjdk:8)

Step 2: Exposing the port 8080:8080 (EXPOSE 8080:8080)

Step 3: Work directory is set to the path (WORKDIR /usr/local/bin)

Step 4: Copied the jar from target folder to bin folder (COPY ./target/docker-0.0.1-SNAPSHOT.jar helloworld.jar)

Step 5: Entrypoint is executed.

Now we have created the image, now we can to deploy it as container.

we are going to use the command

docker run -p 8080:8080 “image name”

Here we are exposing the port and running that image as a container.

Here we have exposed the port 8080:8080 and started the container.

Now let us check the browser,

If noticed, we have used some kind of IP (192.168.99.100) and not Localhost. This is because we have deployed this as container in the docker. the docker machine will have an IP address.

So how to check the ip address,

docker-machine ip 

This will give us the docker ip address,

Code can be downloaded at: Dockerize SpringBoot Application – Hello World Example

Multiple Response Type in RestAPI – JSON, XML

Multiple Response Type in RestAPI – JSON, XML:

RestAPI when implemented by default it returns JSON type. But there are situations where the client might require different content type like XML or Plain Text for the SameAPI. It is generally not a good practice to duplicate the RestAPI calls just for the purpose to content-type change.

In this article let us see an example of how the client might request for the specific content-type to be returned and how can this be handled in a single RestAPI call without duplicating the code.

In RestAPI we have @RequestHeader from which we receive the content-type that has to be returned by the API.

Let us understand with an example,

 

pom.xml

 

Employee.java

 

EmployeeService.java

I have included switch only for JSON and XML, if we want we can include for other formats too – text, xhtml etc

EmployeeController.java

App.java

Output:

Code for download available here

 

 

 

 

 

 

Frequently Performed Operations in List

Frequently Performed Operations in List

In our previous article, we had discussed about List interface, types of lists, internal working along with their advantages and disadvantages. (Please read here).

In this article we can discuss some of the most frequently performed list operations and which has to be remembered at all times.

Arrays.asList:

When we use Arrays.asList, it converts the array elements into a fixed size list. Let us see an example,

Now we have a fixed size list -> stringList.

What will be the output?

Now let us try to add an element “Echo” to the above list,

As a common expectation it should add the element to the list, But unfortunately we will get an exception when we try to add an element to the above list,

We will get the same exception when we try to remove an element from the above list, but why?

But, Why?

When we use Arrays.asList -> it returns a fixed size list which limits to perform add / remove operations.

Updating is possible:

Even if we have the limitation to add / remove an element, we can still update the list as the list size is not going to change.

Here is the sample code,

Now for other examples, let us create 2 lists,

We have 2 lists where 5 and 11 are common elements and we have 2 elements 90 in second list as duplicates.

Max and Min in the list:

How to find the maximum element and minimum element in the list ?

We can use ,

What is the list is empty or if it is a generic list when trying Collections to get min or max?

If the list is empty we will get NoSuchElementException and if the list is generic (list contains mix of string, integers etc…) it will throw ClassCastException.

How can we concatenate or add two lists?

Example, if we can to add both the lists intList1 and intList2 , we can use

This will combine all the elements to both lists together.

How to Concatenate or Add two lists avoiding duplicates?

There are two ways of doing this,

Type 1:

We can use streams to get distinct elements,

Type 2:

 

Replacing all the occurrences of a element in the list:

Consider in the intList2, we have element 90. Now if we want to replace all the occurrences of element 90 with 100 in list2, how can this be done?

This will replace element 90 to 100 in intList2.

How can we make a list Synchronized?

The lists are not thread-safe by default, suppose if we want to make the list threadsafe, we can use

SingletonList vs UnmodifiableList:

We have Collections.singletonList and Collections.unmodifiableList. So what is the difference between these two?

Collections.singletonList:  will take single element or object and makes it an immutable list with that single element or object.

Collections.unmodifiableList: Will make the list as an read-only list, when any add or remove operations are performed, it will throws UnSupportedOperationException

 

 

Java Collections – List – An Depth Analysis

Java Collections – List – An Depth Analysis

What is a List?

It is an ordered collection, which stores elements in a order. List can have duplicate elements.

Types of Lists:

  • LinkedList
  • ArrayList
  • Vector
  • CopyOnWriteArrayList

LinkedList:

It is a linear data structure where each element point to the next element.

What is the internal Implementation of List?
Internal implementation of LinkedList is – Doubly LinkedList.

So what is the difference between Single LinkedList and Doubly LinkedList?

Single Linked List:

In a single linked list, all the elements when inserted will point to the next node of the element and the last element will point to the first element. So whats is the draw back of this, we cant do reversal process, only one way access is possible. Example if we are at element 3, we cant go back to element 2. we need to pass to element 4, then element 1 and then to element 2.

 

Doubly Linked List:

In double linked list, all the nodes are interconnected and can do reversal operations. The last node is connected to first node and same way first one is connected to the last node.

Advantages of Doubly Linked List Over Single Linked List:

  • We can traverse in both the directions
  • We can easily insert a new node before the given node.

Disadvantages of Doubly Linked List over Single Linked List:

  • Extra space required for previous pointer (since it is interconnected one for previous and one for next required)
  • Insertion and deletion requires extra operation as previous pointer and next pointer has to be changed.

Advantages of LinkedList:

Linkedlist does not have initial capacity, the list grows as the elements are added to the list. Insertion and deletion are faster in linkedlist as it uses doubly linkedlist.

Why is Insertion and Deletion faster in LinkedList?

  • Insertion and deletion is faster in LinkedList cause, as we have to update the pointers of the node to the next node

Disadvantages of LinkedList:

  • There is no random access, only sequential access. i.e. if we try to find element “100” and the element “100” is in 20th position. List has to start looking from node 0.
  • More memory is required, as Linkedlist uses pointers to point to next node. Pointers itself will consume memory.

ArrayList:

What is the internal Implementation?

  • It uses Array as internal implementation
  • It has initial size capacity to 10.

Advantages of ArrayList:

  • Manipulation is faster in ArrayList (get operations).

Why get operation is faster in ArrayList?

  • ArrayList has direct references to every element in the list, so it can get the n-th element at constant time.

Disadvantages of ArrayList:

  • Insertion and deletion are more expensive, as all the elements needs to be shifted in the array
  • ArrayList initial size is 10, once the size is reached it will rebuild itself with 50% more space.

Let us see the Difference between – Array, ArrayList and LinkedList

Array

ArrayList

LinkedList

We need to give initial size and it will not grow dynamically

Internal implementation of Array, but grows itself to 50% when capacity is reached

Internal implementation is doubly LinkedList – no initial capacity, list grows as elements added

Get operation is faster in array list

Insertion and deletion is faster in LinkedList

Insertion and deletion is slower, as all the elements needs to be shifted when delete or insertion is happened

Manipulation is slower as it has to transverse starting from the first element

 

Vector:

Vector is similar to arrayList except this is synchronized. (thread-safe)

Note: ArrayList can also be synchronized using utils – Collection.SynchronizedList();

Whats the difference between Vector vs ArrayList?

ArrayList

Vector

When the list limit reached, it increases its size by one and half

When the list size is reached it increases its size by two times

It is not synchronized, but can be made synchronized using Collections.SynchronizedList(list)

By default it is synchronized

 

CopyOnWriteArrayList:

What is CopyOnWriteArrayList?

  • It is a synchronized collection, which allows multiple thread to read values at same time.

How does it work?

  • For every update operation, CopyOnWriteArrayList creates a cloned copy of list and will update and sync with the main list at a point of time as decided by JVM

  • very efficient if you have a List where Iteration outnumber mutation e.g. you mostly need to iterate the ArrayList and don’t modify it too often.

  • No concurrent modification exception thrown, when the data is modified. As the iterator works on list but the updates take place in cloned copy of list.

  • But when you try to remove an element during Iteration, it will throw UnsupportedOperationException.