How To Create A Spring Boot REST Microservice with Docker - Little Big Extra Skip to main content

How To Create A Spring Boot REST Microservice with Docker

Step-By-Step Guide To Create A Spring Boot MVC Microservice With Docker

SpringBoot is a popular spring framework capable of running as a standalone executable. It fits well in microservices architecture where each service is supposed to be running independently. In Docker, each Microservice(Spring Boot rest service) can be installed in a separate container and accessed independently.

In this tutorial, we will

  • Create Spring Boot Project from Maven archetype
  • Run and Test standalone application
  • Create a Docker image from Dockerfile
  • Run Docker container
  • Test the application
Prerequisite: Java SDK, Maven and IDE (Using Eclipse for this tutorial)

Create Spring Boot Project from Maven archetype

  • Create a new Maven Project by going to File -> New -> Others (Maven Project)

Create A Spring Boot REST Microservice with Docker

  • Click Next, as shown below

New Maven Project



  • Select the archetype org.springframework.boot (Group ID) and spring-boot-sample-data-rest-archetype(artifact-id). You need to add remote archetype catalogue to eclipse.

Remote SpringBoot Maven archetype

  • Enter the Group ID and Artifact idA Spring Boot REST Microservice project
  • If everything went fine, you should end up with this.

Project structure


Run and Test standalone application

  • Open Class SampleDataRestApplication and Run as Java Application, wait for couple of seconds (5-10) and in your console tab, you should see some output coming in

Run Java application


  • Check the Port number at the end of the console, usually, it is 8080. If you see the above output Congratulations you have Spring Boot MVC running on your system. Open localhost:8080 in your browser and you should see some JSON output.

Tomcat Port


  • Run your project as Maven install so you do get a jar file in target folder of your project

Maven Install



  • In your target folder, you should see a jar file called microservice-0.0.1-SNAPSHOT.jar

Create a Docker image from Dockerfile

  • Now let us add a docker file in  Project ( This needs to be placed under root directory). The File name should be Dockerfile without any extensions.

Create Docker Image



  • In above file what we are saying is that
    • use Java 8 version
    • write into /tmp directory of docker container
    • add the microservice jar and rename it as user.jar 
    • then run touch command  to update the timestamp of the jar
    •  the last line is for Tomcat optimisation.
  • Now either we can add a maven plugin to build docker image or build it using console commands. For this tutorial to keep things simple let us install it through docker commands.
  • Open Terminal/command Prompt and then navigate to the project root directory and build the docker image ( You need to have docker installed on your system)

(don’t miss “.”  , as it tells docker file is in current directory)

If the above command is successful then use docker images to see if the image has been created

Run Docker container

  • Now we will run this docker image


  • We are asking docker here to run our application on localhost port 9090, and since we know that our Tomcat started on port 8080 so we are pointing to that port within docker. The docker assigns some random name each time docker is restarted so it’s good practice to name the docker image.
  • If above command was successful.Use <code>docker ps -a </code>, it should show the running images in terminal/command prompt.

Test the application

  • Now if you open your browser and type localhost:9090, you should get the same JSON which you got earlier when you were running the application as Java application.

The source code for above project can be found  


Why not connect this application to MongoDB

Follow these 2 videos


This video will show you how to create standalone Spring Boot Rest service project

This video will show you how to build docker image and run a docker container

Related Posts

One thought to “How To Create A Spring Boot REST Microservice with Docker”

  1. Believe me. It works just awesomely.
    Don’t know how many tutorials, I went through. But none of those completed either due to missing instructions or due to docker issues on Windows Environment.

    Many Thanks …!

Leave a Reply

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