Dockerize Springboot Java Application

Project flow:

Initialize spring boot --->Build and Run the application(locally)--->Create Dockerfile --->Run Docker container.

Initilize spring

Initialize spring using the spring initializer website where u can give the name of your project and the java version and add spring web dependency and download the zip file open the unzipped file using IDE.

Set the access port of the application by src->resource-> application properties you will see an empty file where u should add server. port = 5051

Screenshot (44).png

Build Application and Run

Here we just build a small Hello World application go to src->main->java->com.{your_application_name} and add a java class to it and add the below code snippet

Screenshot (47).png

Run the application and access the application in localhost:{portnumber}

Dockerfile

Create Dockerfile in folder FROM openjdk:17

ARG JAR_FILE=target/*.jar

COPY ${JAR_FILE} app.jar

EXPOSE 5051

ENTRYPOINT ["java","-jar","/app.jar"]

now build the docker image using the command docker build -t maven_application .

check using the docker images command and now run the docker image using the command docker run -p 9022:5051 36752f3a29e7

Now we can access the application in localhost:9022.