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.


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


We will first create a SpringBoot project from 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:

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 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 “” 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 ““and command line Runner “” 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.

    name: catalogue-service
      ddl-auto: update
      enabled: true
      show-details: always
        include: '*'
        include: '*'
  port : 8010
    path: /docs
    path: /swagger

Step 9:

Create unit test class 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 

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

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

# Allows you to configure a container that will run as an executable
ENTRYPOINT ["java","","-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 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 –


Try to check if service’s health is good:


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: Logo

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

Facebook photo

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

Connecting to %s