Create Docker Image of Spring Boot Microservices using Fabric8 maven plugin.

Create Docker Image of Spring Boot Microservices using Fabric8 maven plugin.


In this post we will see how to create a Docker image of Spring Boot Microservice using Fabric8 maven plugin.

Steps,

1. First we will create a Spring Boot Microservice with some REST endpoints
2. Run the Spring Boot Microservice and access the endpoints to see if it works.
3. Create the docker image of a Microservice created above.
4. Run the Spring Boot Microservice Docker image to see if we can access the REST endpoints. 

Spring Boot Microservices Hello World Example 

Download the Spring Boot Microservices Hello World Example from GitHub:
spring-boot-docker-hello-world

Layout of the application is as shown below,
spring-boot-docker-fabric8-maven-plugin-example

It is a Spring Boot Microservice exposing only one endpoint "/"

resources/application.properties
Microservice contains application.properties file through which we can define the application context, change the default post on which application is going to deploy. 
In this example we changed the default application port to 8085

Dockerfile
Microservice also contains Dockerfile which contains instructions for the Docker as how to create the image of this Microservice.
FROM adoptopenjdk/openjdk11-openj9:alpine
ADD target/${project.build.finalName}.jar /myapp/${project.build.finalName}.jar
EXPOSE 8080
ENTRYPOINT java -jar /myapp/${project.build.finalName}.jar
Get the base image adoptopenjdk/openjdk11-openj9:alpine, 
Add our microservice jar inside docker image file directory at location "/myapp",
Expose the container port 8080 (this means Docker image container will listen on port 8080)
Start our Microservice jar

Run the Spring Boot Microservices Hello World Example 

Run the SpringBootDockerHelloWorldApp file as Java application.

When you see below logs in console, 
[main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8085 (http) 
[main] c.j.SpringBootDockerHelloWorldApp : Started SpringBootDockerHelloWorldApp 

Go to browser and hit url http://localhost:8085/, you will see response as "Hello JavaByPatel"

Create the Docker image of Spring Boot Microservices using Fabric8 maven plugin

Run the command,

1. mvn clean install
This will create the target directory and place the generated jar of our Microservice inside directory with name "spring-boot-docker-hello-world-0.0.1-SNAPSHOT.jar"

2. mvn fabric8:build
This will create the docker image of the Microservice inside the running Docker.
run the command below to see existing "docker images"

spring-boot-docker-image-example

Inside the target directory, you would see a new folder name Docker containing image tar file.

docker image tar file


Running the Docker image


Run the command,

docker run -p 8085:8085 spring-boot-docker-hello-world:0.0.1-SNAPSHOT.dev

Here we are saying, map the container port 8085 to Tomcat port 8085 running inside container.

spring-boot-docker-hello-world

Go to browser and hit "http://localhost:8085/", it should give the response as "Hello JavaByPatel"

In case if the URL is not reachable, check the IP of docker by issuing the command(I was running Docker toolbox on windows and has to provide IP instead of localhost), 

$ docker-machine ip
192.168.98.112

Go to browser and hit, "http://192.168.98.112:8085/", It should work now.