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

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. 

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. 

https://docs.docker.com/engine/installation

Oralce VirtualBox On Window machines. This is required to start a virtual machine.

https://www.virtualbox.org/wiki/Downloads

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.

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)

/src/main/docker/Dockerfile

The Dockerfile has the following setup:

FROM frolvlad/alpine-oraclejdk8:slim

VOLUME /tmp

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.

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

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

<plugin>

<groupId>com.spotify</groupId>

<artifactId>docker-maven-plugin</artifactId>

<version>0.4.11</version>

<configuration>

<imageName>${docker.image.prefix}/${project.artifactId}</imageName>

<dockerDirectory>src/main/docker</dockerDirectory>

<resources>

<resource>

<targetPath>/</targetPath>

<directory>${project.build.directory}</directory>

<include>${project.build.finalName}.jar</include>

</resource>

</resources>

</configuration>

</plugin>

</plugins>

</build> 

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. 

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 

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

http://199.0.0.101:8080/v0.1/messages

 

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

messaging-0.0.1-SNAPSHOT.jar

 messaging-0.0.2-SNAPSHOT.jar
 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?