Scroll Top

Using Microservices with Docker

Microservice is a small and independent process which will do its defined job. In general, microservice itself utilize less cpu and jvm memory. However, when it is not managed well, it leads to wastage of infrastructure.

Let’s assume that you have 20 microservices and each has different environments like dev, testing, staging and production. To run all these microservices you need more virtual machines or instances. It leads to increase in infrastructure cost, the system becomes more complex and the hardware resource utilization become inefficient.

This is where Docker helps. Docker is a container management technology, which isolate each application and share the resources like CPU & Memory from the hosting machine. Containers need minimal setup to run application like basic operating system and application dependencies. So you can deploy multiple microservices in same virtual machine by using the docker. The result is significantly faster deployment, much less overhead, easier migration, faster restart and better resource utilization.

As part of this article, you will learn how to use microservices with the docker and take you through the following topics

  • What are MicroServices?
  • What is Docker?
  • Understanding the dockerfile
  • Build a docker image with maven

What are Microservices?

The microservice architectural style is an approach to developing a single application as a suite of small services, each running its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around specific business capabilities and they are independently deployable by fully automated deployment machinery.

There are many reasons why this approach is considered an easier way to develop large applications, particular enterprise applications, and various types of software as a service delivered over the Internet.

  1. One of the reasons is from a project engineering perspective. When the different components of an application are separated, they can be developed concurrently.
  2. Another is resilience. Rather than relying upon a single virtual or physical machine, components can be spread around multiple servers or even multiple data centers. If a component dies, you spin up another, and the rest of the application can continue to function.
  3. It allows more efficient scaling. Rather than scaling up with bigger and more powerful machines, or just more copies of the entire application, you can scale up with additional copies of the heaviest-used parts.

Below image shows how the monolithic and microservices based architecture differ with respect to the scaling:

Credit:https://martinfowler.com/articles/microservices/images/sketch.png

What is Docker?

Docker is a platform for developing, shipping, and running applications using container virtualization technology. Container virtualization directly uses the host OS  to run multiple guest instances, which is called a container. Each container is an isolated space that has its own root file system, process, memory, and network ports. Within each container, the application and libraries that the application depend on are installed.

The benefits of using docker is that the containers are lightweight because they don’t need to install the guest OS, and as a result less CPU/RAM/Storage is required for launching each container. Through a DockerFile, you can declaratively define a Docker image to be run, and be assured that your Docker image will run exactly the same way in any environment (dev,test,prod,etc.).

Docker Architecture

Credit :https://docs.docker.com/engine/article-img/architecture.svg

Docker Components:

Docker Client :  

Docker client is a command line tool to run the docker commands to pull/push images to Dockerhub/Private Repository

Docker Commands:

You can find  more docker commands on the following link:

https://docs.docker.com/engine/reference/commandline/docker/

Docker Daemon :

When client user run commands in the docker cli, the Docker daemon receives and processes the incoming docker api requests.

Docker Hub :

Docker hub is an open source repository for storing docker images. After sign up, we can create  private/public repositories in the docker hub.

Understanding the dockerfile

  • Unzip the above downloaded application, it contains the following folder hierarchy
  • Navigate to following path, you can see DockerFile

      

  • Open the above file, you can find the following configurations in the script:

Here is the explanation for above script:

  1. By using FROM command we can get the existing docker image from the docker hub or any other private repository. For example to run microservice we need java, java docker image available in the docker hub.
  2. By default the tomcat will create directory in tmp folder. So volume is given as tmp folder.
  3. ADD command will be used to copy the jar to the docker image
  4. ENTRYPOINT is the execution path for microservice

For more information about docker file references please visit the below URL:

https://docs.docker.com/engine/reference/builder/

Build a Docker Image with Maven

Maven supports the docker plugin in order to build the docker image. The following configurations are required in building the docker image either with maven or gradle.

  • The image name (or tag)
  • The directory in which to find the Dockerfile
  • The resources (files) to copy from the target directory to the docker build (alongside the Dockerfile) – we only need the jar file in this example

In the application downloaded in the previous section, you will find the maven pom.xml, consisting of the following configuration;

Use maven to build the docker image with the following command:

mvn package docker:build

Now, by running the docker image command, you will see following output

Starting the Docker Container

You can start your docker container with the following command:

docker run -p 8080:8080 -t  narendrabandhamneni/gs-spring-boot-docker

Note:

Replace namespace in the above command with your docker hub namespace.

Verifying your Docker Container

Now browse with the IP address of your host and port number 8080 and you can see following output

Above output is coming from the default service in the previously downloaded application. If you open a file Application.java then you would note following code, which send the “Hello Docker World” as output:

In the above code, you can add more api’s and rebuild docker image, re run docker container, you can see the desired output.

Summary

With the popularity of the REST services and the continuous desire to move towards more manageable microservices, it is important to have the right container in place. Docker provides precisely the same. In this article, I have tried to help you build docker image of microservice using maven.

I hope this article was informative and leaves you with a better understanding of building docker image of microservice. At Walking Tree we are excited about the possibilities that Microservices ecosystems bring in. Stay tuned for more articles on this topic.

Related Posts

Comments (1)

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.