Go back to Read free chapters

Running Hashtag Messaging application inside a Docker container

Learn how to use a Docker container with downloadable tutorial Hashtag Messaging container application

AliExpress.com Product - 10 pcs/lot 100MM 150MM 300MM 500mm Servo Extension For Futaba JR Lead Wire Cable RC Parts

How Docker works with Spring Boot?

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. 

AliExpress.com Product - Yeston Radeon RX 550 GPU 4GB GDDR5 128bit Gaming Desktop computer PC Video Graphics Cards support DVI/HDMI PCI-E X16 3.0

Requirements for running an application inside a Docker container

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.

AliExpress.com Product - New Crypto Coin Open Air Mining Miner Frame Rig Case up to 6 GPU ETH BTC Ethereum High Quality computer Case Towers For BTC

Hashtag Messaging application setup for Docker

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:

FROM frolvlad/alpine-oraclejdk8:slim


ADD messaging-0.0.2-SNAPSHOT.jar messaging.jar

RUN sh -c 'touch /messaging.jar'

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/messaging.jar"]

With just this setup you are able to launch the service inside a Docker container.

AliExpress.com Product - 216 Super Magnet Diameter Golden 4mm Neodymium Nickel Magnet Rare Earth Strong Power Magnets For Industry OMO Magnetics

Maven POM.xml configuration for running a Docker package

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

























How to start Docker and run an application inside the container

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. 

AliExpress.com Product - 2016 Newest TEVO Tarantula I3Aluminium Extrusion 3D Printer kit printer 3d printing 2 Rolls Filament 8GB SD card and LCD As Gift

Run a custom application inside Docker container

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 

AliExpress.com Product - Original Raspberry Pi 3 Model B Board 1GB RAM Quad Core 1.2GHz 64bit CPU with WiFi & Bluetooth Function made in UK Free Shipping

Test the new application with the Docker setup

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. 

docker-machine ls

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]:[8080]/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 ps

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.


Download Docker Hashtag Messaging API v0.0.1

Download Docker Hashtag Messaging API v0.0.2.1D

- 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

Download Docker Hashtag Messaging API v0.0.3.1D

- Fixed error in setting default maximum offset for fetching messages. The offset was always 1000.

Download Docker Hashtag Messaging API v0.0.4.2D

Added Validation with twitter-text, custom validator and Java Validators, added HTML character escaping

What new ideas or thoughts this chapter gave you?