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 endpoints2. 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.
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,
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}.jarGet 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 installThis 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:buildThis will create the docker image of the Microservice inside the running Docker.run the command below to see existing "docker images"
Create the Docker image of Spring Boot Microservices using Fabric8 maven plugin
Run the command,
Inside the target directory, you would see a new folder name Docker containing 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
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.
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.
You may also like to see
Advanced Java Multithreading Interview Questions & Answers
Type Casting Interview Questions and Answers In Java?
Exception Handling Interview Question-Answer
Method Overloading - Method Hiding Interview Question-Answer
How is ambiguous overloaded method call resolved in java?
Method Overriding rules in Java
Interface interview questions and answers in Java
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.