Spring boot with Docker

Spring boot with Docker

Nowadays there is a lot of buzz about microservices, spring boot, and docker. Today I am going to explain how to deploy a spring boot application inside Docker container. Before that, I would like to explain a bit about Microservices, Spring Boot, and Docker.

What are microservices?

Microservices is an architectural style of software development where an application is developed as a collection of loosely coupled services. These services can ideally be developed in any programming languages since they talk with each other via HTTP requests. The advantage of microservices is that each service can be developed and deployed independently enabling CI/CD. Ideally, these services have their own databases. Even though Microservices are quite famous nowadays, this architecture might not be suitable for application where modules are interdependent and share a lot of common resources. Now that we know what microservices are, where does Spring Boot comes into the picture?

What is Spring Boot?

As cited from its official website , “Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.” Features enlisted on the website are as below:

  1. Create stand-alone Spring applications
  2. Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  3. Provide opinionated ‘starter’ POMs to simplify your Maven configuration
  4. Automatically configure Spring whenever possible
  5. Provide production-ready features such as metrics, health checks, and externalized configuration
  6. Absolutely no code generation and no requirement for XML configuration

Why use Spring Boot for microservices?

The main reason to use Spring boot I suppose is the familiarity with Spring. Spring framework is quite widely used. Developers are very used to with developing RESTful web services using Spring framework. This helps in migrating existing monolithic applications into Microservices based application. Now that we have chosen Spring Boot for developing microservices and have developed our services, why to deploy them in Docker container? Why not run them like we used to do earlier? To know the answer we will dig up a bit into Docker.

What is Docker?

From its website ‘Docker is the world’s leading software containerisation platform’. A container platform is a solution which provides enterprise operation requirement like security, governance, automation, support and certification over the entire application lifecycle. The main moto of Docker is ‘Build, Ship and run any app anywhere’. To achieve this it provides developer ready to use container and lot of available technology options. The developer doesn’t need to worry about environment setup and can totally focus on business logic. These containers are lightweight and suitable for deploying microservices and hence are often chosen. Let’s check out few concepts docker has introduced:

Concepts in docker:

  1. Docker Image: It works as a template for containers to run. After building every application in docker, corresponding application image is generated. You can share this image with anybody and that person can start running your application. He doesn’t need to setup any application environment like databases.
  2. Dockerfile: This file defines the variables to be set to run the application, for example, application port.

Now that we have seen the overview, let’s build a Spring Boot application and deploy it in Docker.

Creating Spring boot application and running in Docker:

For explaining this, I will create a spring boot application which will handle the request from a user and display some message. Later on, you can add complexity to your project as per requirement.

  1. Creating spring boot application: Let’s first create a Spring Boot application. You can refer to Spring boot website to create a sample application or go to Spring Initializr. Spring also provides a project on Git repository which you can clone Spring Boot Git Project. I have cloned the application from given Git repository. It has two maven projects namely ‘complete’ and ‘initial’. I have chosen ‘initial’ as my project and making changes in corresponding classes inside this directory.
  2. Request mapping in application: In order to handle user requests I have to create a request mapping and return some message. To do so, I annotate my Application.java class with ‘@RestController’ and add request mapping as below:
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class Application {
      public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
      }
      
      @RequestMapping("/")
      public String sayHello(){
        return "Hello Neha!";
      }
    
    }
  3. Creating dockerfile for application: This file will be saved in the root folder of the application. In my case, it will be placed under ‘initial’ directory. Create a file named ‘Dockerfile’ inside the application which will define application parameters like below:
    FROM java:8 -> Tells what java version I am using
    EXPOSE 8080 -> Informs Docker that the container listens on the specified network ports at runtime.
    ADD /target/gs-spring-boot-docker-0.1.0.jar gs-spring-boot-docker-0.1.0.jar -> Copies file from source to docker container’s filesystem
    ENTRYPOINT ["java","-jar","gs-spring-boot-docker-0.1.0.jar"] -> Tells which executable should run when container is started with this image
  4.  Building application in Docker container: In order to build the application in docker, open the Docker terminal; navigate to ‘initial’ directory. Run following commands sequentially:
    1. mvn clean (clears target directory of application)
    2. mvn install (prepares jar file of application)
    3. docker build –f Dockerfile –t spring-docker (Here, ‘Dockerfile’ is the name of dockerfile and ‘spring-docker’ is the name of the image that will be created for this application)
    4. docker images (Command to enlist all images in docker machine) Note: You can check out various docker commands at Docker Commands .
  5. Running the application in docker: To run the above generated image, run the following command:Docker run –p 8080:8080 spring-docker (This command tells Docker to run the image ‘spring-docker’ on the ‘8080’ port of docker. If you don’t specify the port here, docker will choose any of its available port)
  6. Check the IP on which Docker default machine is running: Check the IP on which Docker’s default machine is running and hit it. You can see this Ip once you open the docker terminal:
  7. Verify your application is running: After starting the image if Docker terminal displays log like ‘Started application..’, your application has started successfully and you can verify it by hitting the above IP:8080                                                                                                                                                                                                                             

If you don’t see this on your browser, please verify that you have followed all steps correctly.

 

My Other Related Blogs


2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar