how to dockerize a spring boot 3 application

create project

add web dependency

create controller pkg and class HelloController

HelloController

package com.example.demo.controller;

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

@RestController
public class HelloController {

 @GetMapping("/hello")
 public String sayHello() {
  return "Hello World";
 }
}

run the application check whether working or not

stop application

run application as maven clean

run application as mvn install

refresh project

and inside target you will see the jar file

go to exact location of jar file on file system and open command prompt

and try to run the jar file

stop the application

create Dockerfile

Dockerfile


FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
ADD target/SpringBoot3Docker-0.0.1-SNAPSHOT.jar SpringBoot3Docker-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/SpringBoot3Docker-0.0.1-SNAPSHOT.jar"]

go to exact location of Dockerfile on file system

open command prompt and run below command to create image

>docker build -t springbootdocker:latest .

check whether docker image created or not


>docker images

run the docker image

>docker run -p 3033:8080 springbootdocker