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, h...

/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:/d...

With just this setup you are able to launch the...

Maven POM.xml configuration for running a Docker package

You will also need a new maven plugin to build ...

<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 D...

Run a custom application inside Docker container

You have to launch the Docker instance inside t...LOGIN TO READ FULL ARTICLE

Change the directory to the folder where you ex...

cd /c/[your workspace directory]/messaging

mvn package docker:build

docker run -p 8080:8080 -t springio/messaging&n...

Test the new application with the Docker setup

Press Control + C to return back to the shell. ...

docker-machine ls

The command prints out the list of your Docker ...

http://[YOUR DOCKER CONTAINER IP ADDRESS]:[8080...

The final URL depends on the machine that you'r...

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

 

This test fetches all the fresh message from th...

[]

So go to the previous chapter and do the same P...

To stop the Docker container use the commands&n...

docker ps

docker stop [CONTAINER ID]

 

Now you have the Hashtag messaging API up and r...LOGIN TO READ FULL ARTICLE

 

- Changed messaging API version to v0.1, ...?

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

- Fixed error in setting default maximum offset...

Added Validation with twitter-text...

What new ideas or thoughts this chapter gave you?