Containerizing Java applications | Creating a Spring Boot App using Dockerfile

22 Oct, 2021
The goal of this article is to containerize a Java application easily, by creating a Spring Boot App using Dockerfile.
The steps will be as follows:-
Setting up a spring-boot appCreating a dockerfileBuilding project jarBuilding docker image by using dockerfileRunning image
Setting up a spring-boot app
Creating a dockerfile
Building project jar
Building docker image by using dockerfile
Running image
Let’s examine the above steps in detail:
Setting up spring-boot app: So first of all, use spring initializer to have a very basic spring-boot greetings project with the help of a web dependency.The project includes a simple rest controller with a simple greeting message.To run this app use command:mvn spring-boot:runCreating A Dockerfile: A dockerfile is a text document which contains commands read by docker and is executed in order to build a container image.FROM: The keyword FROM tells Docker to use a given base image as a build base. In this case Java8 is used as base image with jdk-alpine as tag. A tag can be thought as a version.COPY: copying .jar file to the build image inside /usr/app.WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow in the Dockerfile. Here the workdir is switched to /usr/appRUN: The RUN instruction runs any command mentioned.ENTRYPOINT: Tells Docker how to run application. Making array to run spring-boot app as java -jar .jar.Building Project Jar: Now run mvn install to build a .jar file in target directory.Building Docker Image: Execute command docker build -t spring-boot-docker-demo .Run the image build: Execute command docker run spring-boot-docker-demo
Setting up spring-boot app: So first of all, use spring initializer to have a very basic spring-boot greetings project with the help of a web dependency.The project includes a simple rest controller with a simple greeting message.To run this app use command:mvn spring-boot:run
The project includes a simple rest controller with a simple greeting message.
To run this app use command:
mvn spring-boot:run
Creating A Dockerfile: A dockerfile is a text document which contains commands read by docker and is executed in order to build a container image.FROM: The keyword FROM tells Docker to use a given base image as a build base. In this case Java8 is used as base image with jdk-alpine as tag. A tag can be thought as a version.COPY: copying .jar file to the build image inside /usr/app.WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow in the Dockerfile. Here the workdir is switched to /usr/appRUN: The RUN instruction runs any command mentioned.ENTRYPOINT: Tells Docker how to run application. Making array to run spring-boot app as java -jar .jar.
FROM: The keyword FROM tells Docker to use a given base image as a build base. In this case Java8 is used as base image with jdk-alpine as tag. A tag can be thought as a version.
COPY: copying .jar file to the build image inside /usr/app.
WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow in the Dockerfile. Here the workdir is switched to /usr/app
RUN: The RUN instruction runs any command mentioned.
ENTRYPOINT: Tells Docker how to run application. Making array to run spring-boot app as java -jar .jar.
Building Project Jar: Now run mvn install to build a .jar file in target directory.
Building Docker Image: Execute command docker build -t spring-boot-docker-demo .
Run the image build: Execute command docker run spring-boot-docker-demo
Github repository: Spring Boot Docker Demo
ruhelaa48
saurabh1990aror
java-advanced
Java-Spring
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java