Skip to main content

Run Spring boot application on Docker

In this blog, I will explain how to  run the .jar file on Docker Engine

Docker containers are the fastest growing cloud-enabling technology and driving a new era of computing and application architecture with their lightweight approach to bundle applications and dependencies into isolated, yet highly portable application packages.

What you need?

  • Docker running on your computer.
Initial Plan

I will create a simple spring boot application and then create a Dockerfile to build the docker image. finally, I will run the application on Docker container

If you don't have Docker running on your computer. Here is the link to install the Docker 



Step 1: Create Spring-boot application

Navigate this link to create the initial spring-boot application.

Import the project to the Eclipse IDE or whatever you like and create the simple rest endpoint. Here is my code.

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest/docker/hello")
public class HelloController {

 @RequestMapping
 public String hello() {
  return "Hello Docker...!";
 }
}
application.properties

server.port=8085

Build the project to create the .jar file. I have the .jar file in the target/docker-jar.jar

Step 2: Create the Dockerfile

I have created the Dockerfile in the project folder


FROM openjdk:8
ADD target/docker-jar.jar docker-jar.jar
EXPOSE 8085
ENTRYPOINT ["java", "-jar", "docker-jar.jar"
Step 3: Build the Docker image and run the application on Docker

Use below command to build the docker image from the Dockerfile

docker build -f Dockerfile -t docker-spring-boot .
Check the docker images using the below command


docker images
output:
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
docker-spring-boot   latest              73f02cac7fca        About an hour ago   640MB
<none>               <none>              f410a262a2df        About an hour ago   640MB
openjdk              8                   5f4603da3fbc        12 days ago         624MB
hello-world          latest              fce289e99eb9        4 weeks ago         1.84kB
Okay, Let's run Docker image


docker run -p 8085:8085 docker-spring-boot
Now the spring-boot Application is running on the docker.

check the http://localhost:8085/rest/docker/hello, you will see this output "Hello Docker...!"

Comments

Popular posts from this blog

How to use WSO2 Class Mediator in WSO2 ESB

The  Class Mediator  creates an instance of a custom-specified class and sets it as a mediator. If any properties are specified, the corresponding setter methods are invoked once on the class during initialization. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.  The syntax of Class Mediator in ESB < class   name= "class-name" >     <property name= "string"   value= "literal" >     </property> </ class > Creating a Class Mediator lets use the Eclipse  WSO2 Developer Studio Create a New  Mediator project by selecting File --> New --> project --> Mediator Project Now you have class mediator by extending the AbstractMediator class. Then you need to implement the mediate methods Sample class mediator implementation is as follows. package lk.harshana; import org.apache.synapse.Mess

One to Many Mapping using Spring boot

In this blog, I will explain how to use one-to-many mapping in Spring boot Application What you need? JAVA MySql Eclipse IDE ( whatever you like IDE, I'm using Eclipse for this example) Maven ( you can use the Gradle as well) Initial Plan I will create a spring boot application project using the  Spring Initializer  web tool and import the project as a maven project. after configuring the all necessary setting, I will code for one-to-many mapping. Below diagram is the database model diagram which we going to install using the spring boot application. Let's Start to Code. You need to configure the application.properties file for database connections. add the following content to the src/main/resources/application.properties spring.datasource.url=jdbc:mysql://localhost:3306/learning spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate

When To Use Indexes In MySQL

When deciding when and how to create an index in your MySQL database, it's important to consider how the data is being used. Let's say you have a database of  students . We will create it like this: CREATE TABLE `students` ( `id` int ( 11 ) NOT NULL AUTO_INCREMENT , `first_name` varchar ( 255 ) DEFAULT NULL , `last_name` varchar ( 255 ) DEFAULT NULL , `class` varchar ( 255 ) DEFAULT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as "order by". You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding students, you don't have to worry about the inserts Let's say that you will be looking up the students with a web interface and the end user will be typing in the students name to find them, since r