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

Understanding C1 and C2 Compilers in Java

Understanding C1 and C2 Compilers in Java Understanding C1 and C2 Compilers in Java In Java, the Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. The JIT compiler includes two different compilers, known as the C1 and C2 compilers, each with distinct optimization strategies and purposes. C1 Compiler (Client Compiler) The C1 compiler, also known as the client compiler, is designed for fast startup times and lower memory consumption. It performs lighter and quicker optimizations, which makes it suitable for applications that require quick startup and responsiveness. Key characteristics of the C1 compiler include: Quick Compilation: Prioritizes fast compilation times over deep optimizations. Low Overhead: Consumes less memory and resources during compilation. Profile-Guided Optimization: Ca...

Understanding -XX:+PrintCompilation Output in Java

Understanding -XX:+PrintCompilation Output in Java Understanding -XX:+PrintCompilation Output in Java The -XX:+PrintCompilation flag in the Java Virtual Machine (JVM) prints information about the methods being compiled by the Just-In-Time (JIT) compiler. When you enable this flag, the JVM will output a log of compilation events to the standard output. Each line of the output provides information about a specific method being compiled. Here, I'll explain the meaning of the different columns and markers, specifically focusing on the n , s , and % markers as seen in your example. Explanation of Output Columns and Markers Here's a breakdown of what each column and marker means: Timestamp : The time (in milliseconds) since the JVM started when the compilation event occurred. Compilation ID : A unique identifier for each compilation task within the JVM's lifecycle. Optimization Level : The lev...

What is L1, L2 and L3 Support Engineering

In this blog article, I'm going to explain about the Software support engineering role with my experience. I was a Level 2 and 3 support Engineer during my career. L1 - Level 1 L2 - Level 2 L3 - Level 3 Ticket - Incident L1 support includes interacting with customers, understand their issue and create tickets against it. The ticket then routed to the relevant L2 support ( Integration support, Server & Storage support, etc ...). L1 support Engineers have basic knowledge of product/service and skill to troubleshoot a very basic issue like password reset, software installation/uninstallation/reinstallation. L2 support manages the tickets which routed to them by L1( L2 support also can create tickets against any issue notice by them). They have more knowledge, more experience in solving related complex issues and can guide/help L1 support folks job in troubleshooting. If the solution not provided at this level then escalate to the L3. L3 is ...