Spring Boot + External Tomcat + Docker Package Deployment
Spring Boot + external tomcat + docker package deployment Introduction
Spring Boot.Preface Recently, I was doing a "Docker Graceful shutdown", so I started to do various " small experiments". Although the process is a bit painful, the results are always good, and I have some new understanding of docker.
Spring Boot.foreword
Spring Boot.Recently, I was doing a "Docker Graceful shutdown", so I started to do various " small experiments".
Although the process is a bit painful, the results are always good, and I have some new understanding of docker.
Spring Boot.text
Closer to home, today's topic is Spring Boot + external tomcat + Docker, and then you can deploy it with a ready-made image.
We need to modify the two major blocks:
1.Spring boot
2.Docker
Spring Boot.I didn't leave tomcat down. The reason is that we can make docker reference tomcat's image.
First, let's transform the Spring boot project to "get rid of" the built-in tomcat
pom.xml:
< 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
< exclusions >
< exclusion >
< groupId > org.springframework.boot
< artifactId > spring-boot-starter-tomcat
< dependency >
< groupId > org.springframework.boot
< artifactId > spring-boot-starter-tomcat
< scope > provided
启动类:
package com.demo.externaltomcattest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExternaltomcattestApplication {
public static void main(String[] args) {
SpringApplication.run ( ExternaltomcattestApplication . class , args ) ;
}
}
the same level as the startup class, we add a class called ServletInitializer :
package com.demo .externaltomcattest ;
import org.springframework.boot.builder.SpringApplicationBuilder ; _
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer ;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ExternaltomcattestApplication.class);
}
}
The key is the above three steps.
If you don't understand, you can refer to my github : https://github.com/luckypoison/SpringBootTomcatExternalTest .
️Just pay attention to the places I mentioned above, the rest is what I use for grace-shutdown testing ️.
________________________________________
Next, we need to type out a war package for deployment. If you don’t know what a war package is, you can search for it yourself.
Use the following command to package:
mvn clean package
Then we can find the war package under the target directory under the current project directory.
________________________________________
dockerfile and our war package in an empty folder .
the Dockerfile are as follows:
FROM tomcat: 9.0
COPY externaltomcattest-1.0.war / usr / local /tomcat/ webapps
EXPOSE 8080
CMD [ "catalina.sh" , "run" ]
Here's a basic explanation:
1.The FROM instruction tells us which basic image Docker should use
2.Copy our war package to a path of tomcat in docker.
3.Expose port 8080
4.start tomcat
Then use the following command to build:
First we need to go to the folder we just created.
docker build -t external-tomcat-test:1.0 .
-t is followed by the name and version number of the image we need to package.
️The "." after it is indispensable.
Then use the following command, if there is the image we just packaged in it, it proves that our build is successful:
docker images
Finally start it.
docker run -p 8080:8080 < imageId >
If necessary, you can put the build image and launch where you need it.