In this article, we will learn how to dockerize a spring boot 3 application. If this is something you’re unfamiliar with, no worries, we’ll go step-by-step and build the application from scratch.

You can find a youtube video below with my step-by-step process.

https://www.youtube.com/watch?v=TWlrwkUZgqE&feature=youtu.be&embedable=true&transcript=true

Let’s dive straight into the steps and code

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

The lead image for this article was generated by HackerNoon's AI Image Generator via the prompt "spring boot".