Create a Microservice using SpringBoot, dockerize and run on Docker

Disclaimer: This blog content has been taken from my latest book:

“Cloud Native Microservices with Spring and Kubernetes”

In this blog, we will create a simple Microservice using SpringBoot and Java. We will create docker file of this microservice using Maven, deploy and run on Docker using Docker Desktop on local machine.

Note: This POC has been done on Mac machine.

You can also try on Windows machine with a small configuration.

Prerequisite:

  1. Install Docker Desktop
  2. Install Java
  3. Install Maven
  4. Install Git
  5. Install Homebrew

Step:1

We will first create a SpringBoot project from https://start.spring.io/ and download this project on local machine.

Step 2:

Add required dependencies by clicking on the button ” Add Dependencies”. In my case, I have selected:

  1. Spring Data
  2. Spring Micrometer
  3. Spring Data JPA
  4. Lombok
  5. H2 in-memory database
  6. Spring Boot DevTools

Note: Refer this pom.xml file for dependencies and Maven plugins and repositories:

https://github.com/rajivmca2004/catalogue-service/blob/master/pom.xml

Step 3:

Download this project artefact on your system by clicking on the “Generate” button. It will have initial template, Maven style directory structure, POM.xml with all required dependencies and default application.yaml configuration file which is required SpringBoot with main class CatalogServiceApplication.java file.

Step 4:

Export this project as a Maven project in your IDE (Eclipse, STS, InelliJ etc). I love STS IDE and same will be demonstrated here. You can right click on “Project Explorer” window and select “Export” and select project folder which you have downloaded from Spring Initialiser portal. You are set to write business code and additional configuration for Dockerization.

Step 5:

Create a controller “CatalogueController.java” for your SpringBoot application and add your REST API endpoint definition. Create these REST APIs:

  1. /cataloguedisplay
  2. /catalogue

Step 6:

Create a model Catalogue.Java class to persist Catalogue data model (hard-coded) into local H2 database in-memory. I have used Lombak to avoid writing getters, setters and avoid other overrides methods.

Step 7:

Create repository “CatalogueRepository.java“and command line Runner “CatalogueRepoCLR.java” to persist product catalogue data to H2 database.

Step 8:

Create SpringBoot application configuration file application.yml and add custom available port, otherwise it will take default 8080.

spring:
  application:
    name: catalogue-service
  jpa:
    hibernate:
      ddl-auto: update
management:
  endpoint:
    health:
      enabled: true
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'
    jmx:
      exposure:
        include: '*'
server:
  port : 8010
springdoc:
  api-docs:
    path: /docs
  swagger-ui:
    path: /swagger

Step 9:

Create unit test class CatalogueServiceApplicationTests.java to test basic functionality during build time.

Step 10:

Create a docker file with same name Dockerfile and add these configuration to convert this Java/Jar artefact to docker container. Docker follow step by step, please refer all the comments:

# Docker Image name: catalogue-service-app-demo

# Use the official image as a parent image.
FROM openjdk:8-jdk-alpine

# Creates a mount point with the specified name 
VOLUME /tmp

# variable that users can pass at build-time to the builder with the docker build command 
ARG JAR_FILE

# Add files into docker image
ADD ${JAR_FILE} app.jar

# Allows you to configure a container that will run as an executable
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Step: 11

 Install Docker Desktop:

Install Docker Desktop. The docker CLI requires the Docker daemon, so you’ll need to have that installed and running locally.

Create a Docker Hub account on https://hub.docker.com/ and get set go!

You can create docker images locally, then you have choice to push images to Docker Hub cloud SAAS or set a local Harbor private repository for security reason

Step: 12

Create docker image inside project folder by using Maven build tool. It will download all the dependent libraries from public Maven Central repository and Docker hub SAAS portals. You can use your private image registry and local library repo using Sonatype Nexus or other tools.

cd catalogue-service

#Build a docker image using Maven
mvn clean install dockerfile:build

#List all Docker images:
docker image ls

#Show a breakdown of the various layers in the image:
docker image history catalogue-service:latest

Step: 13

Now, we can runt the application on Docker and expose on the same port which we have defined inside the application.yml

Running a Container Docker Image:

docker run -p 8010:8010 itsrajivsrivastava/catalogue-service:latest

Step: 14

Now, test application by –

http://localhost:8010/catalogue

Try to check if service’s health is good:

http://localhost:8010/actuator/health

Published by

Rajiv Srivastava

Principal Architect with Wells Fargo

One thought on “Create a Microservice using SpringBoot, dockerize and run on Docker”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s