In this tutorial you’ll see how to build a Docker image for running a Spring Boot application. We’ll create a basic DockerFile to dockerize a Spring Boot MVC application where view is created using Thymeleaf.
Maven Dependencies
Since we are creating a web application so we need a spring-boot-starter-web, for Thymeleaf we need spring-boot-starter-thymeleaf, spring-boot-maven-plugin is also added to our pom.xml. This plugin provides many convenient features-
- It helps to create an executable jar (über-jar), which makes it more convenient to execute and transport your service.
- It also searches for the public static void main() method to flag the class having this method as a runnable class.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.knpcode</groupId> <artifactId>SprinBootProject</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootProject</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Classes for Spring Boot Web Application
We’ll add a simple controller for our web application.
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MessageController { @GetMapping("/") public String showMessage(Model model) { model.addAttribute("msg", "Welome to Docker"); return "message"; } }View class (Thymeleaf template)
In src/main/resources added a new folder Templates and in that created a message.html file.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Spring Boot With Docker</title> </head> <body> <div> <p th:text="${msg}"></p> </div> </body> </html>
Application Class
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootProjectApp { public static void main(String[] args) { SpringApplication.run(SpringBootProjectApp.class, args); } }Running the application
You can run this Spring Boot web application as a stand alone Java application but we'll run it by creating an executable jar.
For creating a completely self-contained executable jar file run mvn package from the command line. Note that you should be in your Spring Boot project directory.
knpcode:SprinBootProject$ mvn package
To run application using the created jar, you can use the java -jar command, as follows-
java -jar target/SprinBootProject-0.0.1-SNAPSHOT.jar
But we’ll do the samething by creating a DockerFile.
DockerFile
For running in your application in Docker container you need to create an image which is a read-only template with instructions for creating a Docker container.
For creating Docker image you create a Dockerfile which is a text file with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image.
Create a text file with in your project directory named DockerFile and copy the following text in it.
FROM openjdk:8-jdk-alpine ARG JAR_FILE=target/SprinBootProject-0.0.1-SNAPSHOT.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]
- Often, an image is based on another image, with some additional customization. This is true in our case too and the base image used here is openjdk:8-jdk-alpine This image is based on the popular Alpine Linux project which is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.
- Then assign a name to the jar path.
- Copy jar file.
- Execute jar using the ENTRYPOINT instruction by providing arguments in the following form- ENTRYPOINT ["executable", "param1", "param2"] Which makes it equivalent to java -jar target/SprinBootProject-0.0.1-SNAPSHOT.jar
Create a docker image
You can create a Docker image by running command in the following form-
sudo docker build -t name:tag .
For our project command to create a docker image-
sudo docker build -t sbexample:1.0 .
. means using the current directory as context
tag the image as sbexample:1.0
To create a container (run an image)
The docker run command must specify an image to derive the container from.
sudo docker run -d -p 8080:8080 sbexample:1.0
Here options are-
-d To start a container in detached mode (to run the container in the background)
-p Publish all exposed ports to the host interfaces
If every thing works fine then you will have a dockerized Spring Boot application at this point which you can access by typing URL http://localhost:8080/ in a browser
If you want to see the running containers use following command
sudo docker ps
To stop a running container use following command
sudo docker stop container_id
That's all for the topic Spring Boot With Docker Example. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Spring Boot + Data JPA + Oracle One to Many Example
- Spring Boot Microservices Eureka + Ribbon
- Spring Boot MVC Form Validation Example
- Injecting Prototype Bean into a Singleton Bean in Spring
- Spring Bean Definition Inheritance
- Java Callable And Future With Examples
- Serialization Proxy Pattern -readResolve() and writeReplace()
- Java BigDecimal Class With Examples
- HTTP POST Method in React - fetch, Axios
- super() in Python With Examples
No comments:
Post a Comment