top of page

Programmers T-shirts

Build your own Microservice

Updated: Mar 1, 2021

This tutorial covers how to create a Microservice with Java and Spring boot.

What are we going to build?

  1. A Currency API with Spring boot, Java, and Postgres DB. I already covered it in detail in the Build your own REST API tutorial - make sure to read that first.

  2. The program is going to be packed into a Docker Image using a Docker File.

  3. Docker-Compose to power up the Microservice and the Postgres DB together.

What is a Microservice?

It's a system architecture. Instead of having one giant system, the whole system is divided into small pieces.

Main benefits of such architecture:

  1. Every unit is focused on a specific business logic

  2. Faster development and deployments - each unit is independent

  3. Easy to test

Currency API - I already explained how to create the Currency API in the previous tutorial. Please follow it first, since this tutorial is built right over it.

Here is a brief explanation of the service:

The API will allow to add a new currency, get all currencies, and ask for basic health check.

Here are the URLs for the available endpoints:

When calling the getAllCurrencies for example, we get:

"currencyName: "Euro", 
"price": 1.21
"currencyName: "AUD", 
"price": 0.81


In the root project, we have a "docker" folder with a Dockerfile.

The Dockerfile is used to describe the layers of the Docker Image. We will use it later to create the image by running a docker command against it.

The Dockerfile contains the following content:

This Dockerfile describes the layers of the Docker Image.

The Image is based on the OpenJDK alpine Linux OS. It has Java 8 - we need Java to run our program.

Next, we copy the Currencies-API jar that our SW produces when we run the Gradle Build command.

The Jar produced by Gradle is located at build/libs.

Last, we tell Docker to run app.jar with java and -jar arguments.

Create Your Image

To create our Image we need to perform the following actions:

1. Pack our program into a Jar file:

./gradlew clean build

2. Create an Image using a Docker command

docker build -t currencies-api -f ./docker/Dockerfile .

We tell Docker to create an image with the name ״currencies-api״ using the Dockerfile.

At this point you should be able to type "docker images" in the terminal and see the currencies-api image:


currencies-api latest 38d328414fe8 1 minute ago 123MB


Let's spin up the Container. remember - the Currencies-API cannot work without a Postgres Database, we cannot just run a Postgres container and a Currencies-API Container, they need to be able to communicate under the same network.

The docker-compose was built exactly for this case.


This file describes the composition of the containers with their configurations.

Under "services" we define the containers.

Each container has its configurations - Image Name, Container Name, Exposed Ports, Environment Variables, and more.

By default, docker-compose assigning all Containers under the same bridge network which is exactly what we need.

Notice - when we are running under a subnetwork, the Currencies-API will not be able to find Postgres on This IP is the localhost IP of the Host machine (and we are not running on the Host anymore).

By default, any container will get an IP with the name of its Container, hence we know for sure that Postgres IP is "postgres".

On line 24, we pass the Currencies-API an Environment Variable ip=postgres. The Currencies-API will use it to perform the connection.

In order to spin up the containers we execute

docker-compose up

You should be able to run "docker ps" and see both Currencies-API and Postgres running together.

Test your API

Go to http://localhost:8080/api/healthCheck and you should be able to get a response from the Currencies-API Container.


Practical Programming Tutorials for Developers

Work Desk

The SW Developer

The SW Developer was built in order to provide Practical and Simple Tutorials for programmers.

Created by Dotan Raz, Michael Rodov & Kobi Atiya

  • github
  • LinkedIn


Programmers T-shirts

bottom of page