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)
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)
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>
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>
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
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. .. ..
You can see that it has started.
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