In this chapter we introduce a new way of running the Java Spring Boot microservice: Inside a Docker container. You will get to download the source code, compile the service and launch it inside an own virtualized server that runs inside your host machine. Docker is currently our choise of technology for launching the service. Docker is an architecture that can be utilized for running a new virtualized computer instances. This means that it has it's own memory and disk where the service will be installed in.
These instructions are tested only on a Windows host machine, they may apply also to Mac and Unix based systems. The commands running the container should apply to all environments.
You have to have the following setup installed on your host machine.
Oralce VirtualBox On Window machines. This is required to start a virtual machine.
You will also need the source code project from the end of the article to compile, run and install as a microservice inside the container.
Code Build and Run a new Microservice Spring boot application
The Docker needs a recipe file for the setup, how the new machine is built. Add the following file to your Spring Boot project. (The file already exists in the source code of this article)
The Dockerfile has the following setup:
ADD messaging-0.0.2-SNAPSHOT.jar messaging.jar
RUN sh -c 'touch /messaging.jar'
With just this setup you are able to launch the service inside a Docker container.
You will also need a new maven plugin to build and run the microservice inside Docker container with Maven. In the project's POM.xml file add the following default setup in the file
Now that you have installed the Docker or the Docker Toolbox you need to access it. On Windows run the Docker Quickstart Terminal application and it sets up the Docker default machine and opens up a new terminal command screen.
You have to launch the Docker instance inside this terminal on Windows.
Change the directory to the folder where you extracted the source codes included in this article. Run the docker build with Maven and launch the Docker container. Use the following commands:
cd /c/[your workspace directory]/messaging
mvn package docker:build
docker run -p 8080:8080 -t springio/messaging
Press Control + C to return back to the shell. The container is still running in the background. After that you can list your containers with the following.
The command prints out the list of your Docker containers. After that you can test that the container works by heading to the IP address of the Docker container (Your Docker container IP address) and Port (8080) that you specified in the application.properties settings. Open a browser window and head to for example
http://[YOUR DOCKER CONTAINER IP ADDRESS]:/v0.1/messages
The final URL depends on the machine that you're using, it could be for example
This test fetches all the fresh message from the application so the actual result on the page is
So go to the previous chapter and do the same POST requests to add new messages with hashtags to this IP address.
To stop the Docker container use the commands
docker stop [CONTAINER ID]
Now you have the Hashtag messaging API up and running as a microservice. Next we will see how to build an Android and iOS mobile application that uses the Hashtag messaging API. This way you are able to connect a mobile application to the backend service.
- Changed messaging API version to v0.1, fixed the HQL query by adding the alias e to the select sentence?
|Old version v0.0.1||New version v0.0.2.1D|
Dockerfile JAR version
|API version /v0.0.1/messages||/v0.1/messages|
- Fixed error in setting default maximum offset for fetching messages. The offset was always 1000.
- Added Validation with twitter-text, custom validator and Java Validators, added HTML character escaping