[JAVA] Image Spring Boot app using jib-maven-plugin and start it with Docker

Introduction

This article is the 23rd day of Java Advent Calendar 2019 --Qiita. Do you guys use containers? ?? With the jib-maven-plugin published by Google, you can easily turn an application written in Java into a Docker image. This time I will try to build an image of a Spring Boot application using Spring Boot / Maven and launch a container (I really wanted to do AWS deployment, but I gave up because I do not have time)

environment

Build a Spring Boot application

Create a project built with SpringBoot / Maven. You can easily build an application by using Spring Initializr provided by Spring. Click here for how to use (I will update the usage later)

Add sample controller

Since we will create a web application this time, we will add a sample Controller and html. IndexController

@Controller
public class IndexController {

	@GetMapping("/")
	public ModelAndView get(ModelAndView mav) {
		mav.setViewName("index");
		return mav;
	}
}

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>JibSampleApp</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
	integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
	crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
	integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
	crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script
	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
	integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
	crossorigin="anonymous"></script>

</head>
<body>
	<div class="container">
		<div class="row">
			<div class="jumbotron">
				<h1>JibSampleApp</h1>
				<p>this app is JibSampleApp</p>
			</div>
			<div class="panel panel-success">
				<div class="panel-heading">blank</div>
				<div class="panel-body"></div>
			</div>
		</div>
	</div>
</body>
</html>

Set plugin

Add the following plugin to the pom of the Spring Boot application created above. pom.xml

<plugin>
	<groupId>com.google.cloud.tools</groupId>
	<artifactId>jib-maven-plugin</artifactId>
	<version>1.8.0</version>
	<configuration>
		<to>
			<image>jibsampleimage</image>
		</to>
	</configuration>
</plugin>

run build

With Docker running mvn compile jib:dockerBuild By executing, the image build of the Spring Boot application will be executed. If you have been deprived of administrator privileges on Windows etc., it may not work properly. In that case, you need to temporarily grant administrator privileges. If the build is successful, you can see that the created image is in the Docker image list. docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jibsampleimage      latest              bcb3c6749044        49 years ago        161MB

Start Docker container

Create a Docker container based on the image built above and execute it. By entering the following command, you can map the local 8080 port and Docker 8080 and access it from the browser to check it. docker run -p 8080:8080 -it jibsampleimage

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

2019/12/18 14:11:53.177 [main] INFO   c.a.j.s.JibSampleApp Starting JibSampleApp on be8d974734e4 with PID 1 (/app/classes started by root in /)
2019/12/18 14:11:53.189 [main] INFO   c.a.j.s.JibSampleApp No active profile set, falling back to default profiles: default
2019/12/18 14:11:56.123 [main] WARN   o.m.s.m.ClassPathMapperScanner No MyBatis mapper was found in '[com.atu496.jib.sample]' package. Please check your configuration.
2019/12/18 14:11:58.065 [main] INFO   o.s.b.w.e.t.TomcatWebServer Tomcat initialized with port(s): 8080 (http)
2019/12/18 14:11:58.166 [main] INFO   o.a.c.h.Http11NioProtocol Initializing ProtocolHandler ["http-nio-8080"]
2019/12/18 14:11:58.213 [main] INFO   o.a.c.c.StandardService Starting service [Tomcat]
2019/12/18 14:11:58.217 [main] INFO   o.a.c.c.StandardEngine Starting Servlet engine: [Apache Tomcat/9.0.26]
2019/12/18 14:11:58.535 [main] INFO   o.a.c.c.C.[.[.[/] Initializing Spring embedded WebApplicationContext
2019/12/18 14:11:58.537 [main] INFO   o.s.w.c.ContextLoader Root WebApplicationContext: initialization completed in 5194 ms
2019/12/18 14:11:59.277 [main] INFO   o.s.s.c.ThreadPoolTaskExecutor Initializing ExecutorService 'applicationTaskExecutor'
2019/12/18 14:11:59.532 [main] INFO   o.s.b.a.w.s.WelcomePageHandlerMapping Adding welcome page template: index
2019/12/18 14:12:00.865 [main] INFO   o.a.c.h.Http11NioProtocol Starting ProtocolHandler ["http-nio-8080"]
2019/12/18 14:12:01.033 [main] INFO   o.s.b.w.e.t.TomcatWebServer Tomcat started on port(s): 8080 (http) with context path ''
2019/12/18 14:12:01.050 [main] INFO   c.a.j.s.JibSampleApp Started JibSampleApp in 9.135 seconds (JVM running for 10.809)

When you actually access localhost: 8080. .. .. スクリーンショット 2019-12-18 23.14.07.png

You can see that it has started.

At the end

I was able to create a simple image of the Spring Boot application and start the container. However, this is still a local story, so as soon as I have time, I plan to summarize how to deploy to the container execution environment of AWS, GCP, Azure (originally that was the main one ...) The source code created this time is uploaded on Github.

Recommended Posts

Image Spring Boot app using jib-maven-plugin and start it with Docker
Create a portfolio app using Java and Spring Boot
Spring Boot starting with Docker
Maybe it works! Create an image with Docker and share it!
Create an app with Spring Boot 2
Create an app with Spring Boot
Spring Boot gradle build with Docker
Build Spring Boot + Docker image in Gradle
HTTPS with Spring Boot and Let's Encrypt
Start web application development with Spring Boot
Asynchronous processing with Spring Boot using @Async
Create a simple search app with Spring Boot
How to make Spring Boot Docker Image smaller
Create a Spring Boot development environment with docker
Testing JPA entities and repositories using Spring Boot @DataJpaTest
[Note] Configuration file when using Logback with Spring Boot
Switch environment with Spring Boot application.properties and @Profile annotation
Try using OpenID Connect with Keycloak (Spring Boot application)
Spring Security usage memo: Cooperation with Spring MVC and Boot
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
Spring Boot with Spring Security Filter settings and addictive points
Create Restapi with Spring Boot ((1) Until Run of App)
Until you start development with Spring Boot in eclipse 1
Until you start development with Spring Boot in eclipse 2
Attempt to SSR Vue.js with Spring Boot and GraalJS
Database environment construction with Docker in Spring boot (IntellJ)
Connect Spring Boot and Angular type-safely with OpenAPI Generator
Download with Spring Boot
Hello World (console app) with Apache Camel + Spring Boot 2
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Register your own Docker image with ECR using AWS CLI
Make Docker confusing with Pokemon and make it easier to attach
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
Build Docker Image lightweight and fast with CodeBuild with Santa Banner
Connect to database with spring boot + spring jpa and CRUD operation
Implement REST API with Spring Boot and JPA (domain layer)
Java: Start WAS with Docker and deploy your own application
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
I tried to get started with Swagger using Spring Boot
8 things to insert into DB using Spring Boot and JPA
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Using Mapper with Java (Spring)
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Docker × Spring Boot environment construction
Add module with Spring Boot
Getting Started with Spring Boot
Try using Spring Boot Security
Create microservices with Spring Boot
Send email with spring boot