In recent times, containerization has become a trend. A server can virtualize multiple containers, and at the same time provide services, share hardware resources, and save costs. The leader in containerization is Docker. All microservices releases in our company have been containerized. spring boot also follows the trend and adds Docker's maven plug-in, which can create images by executing commands.
The main content of this section is not about code, but about this Docker plugin. Without further ado, go to pom
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.12</version> <configuration> <!-- Note that imageName must comply with the regular [a-z0-9-_.], otherwise the build will not be successful-> <!-- See: https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed--> <imageName>spring-boot-docker-start</imageName> <!--Equivalent to from java, local image is used locally, if not, pull from the remote repository --> <baseImage>java</baseImage> <exposes> <!--Expose port 8080 in the container --> <expose>8080</expose> </exposes> <!--Enter point, command--> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> imageName is the name of the image. baseImage is the basic image. If there is a local image, if not, pull it from the remote repository, expose port 8080 in the container, execute the java -jar command, and start the microservice. We know that using Docker requires creating a Dockerfile file, and the elements inside are completely reflected through the maven plugin tags. There is still a prerequisite, you have to install Docker first. At this point, we start running
Step 1: Execute mvn clean package docker:build to create a generated image.
Step 2: Start the mirror docker run -it -P spring-boot-docker-start and check the logs in the container
➜ spring-boot-docker-start git:(master) docker run -it -P spring-boot-docker-start . ____ _ __ _ /// ___'_ __ _ _(_)_ __ __ _ / / / / ( ( )/___ | '_ | '_ | / / /` | / / / / / / / / / / / / | | | | | | | | | | | | | | | | | | | | (_| | ) ) ) ) ' |____| .__| |_| |_| |_/__, | / / / / / / ===========================================___/=/_/_/ :: Spring Boot :: (v1.3.5.RELEASE)2018-03-25 08:41:56.274 INFO 1 --- [ main] com.shuqi.ApplicationMain : Starting ApplicationMain on 075543f8f5b6 with PID 1 (/spring-boot-docker-start.jar started by root in /)2018-03-25 08:41:56.287 INFO 1 --- [ main] com.shuqi.ApplicationMain : No active profile set, falling back to default profiles: default2018-03-25 08:41:56.406 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@126d28d3: startup date [Sun Mar 25 08:41:56 UTC 2018]; root of context hierarchy2018-03-25 08:41:58.356 INFO 1 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2018-03-25 08:41:58.382 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat2018-03-25 08:41:58.384 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.332018-03-25 08:41:58.512 INFO 1 --- [ost-startStop-1] occC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2018-03-25 08:41:58.512 INFO 1 --- [ost-startStop-1] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 2113 ms2018-03-25 08:41:58.920 INFO 1 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2018-03-25 08:41:58.928 INFO 1 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2018-03-25 08:41:58.937 INFO 1 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2018-03-25 08:41:58.937 INFO 1 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2018-03-25 08:41:58.938 INFO 1 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2018-03-25 08:41:59.406 INFO 1 --- [ main] swsmmaRequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@126d28d3: startup date [Sun Mar 25 08:41:56 UTC 2018]; root of context hierarchy2018-03-25 08:41:59.516 INFO 1 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.shuqi.controller.HelloController.hello()2018-03-25 08:41:59.523 INFO 1 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2018-03-25 08:41:59.524 INFO 1 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2018-03-25 08:41:59.584 INFO 1 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-03-25 08:41:59.585 INFO 1 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-03-25 08:41:59.645 INFO 1 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-03-25 08:41:59.754 INFO 1 --- [ main] osjeaAnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-03-25 08:41:59.834 INFO 1 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2018-03-25 08:41:59.838 INFO 1 --- [ main] com.shuqi.ApplicationMain : Started ApplicationMain in 4.084 seconds (JVM running for 5.012)[2018-03-25 08:41:59] server started! Started successfully.
Step 3: Enter docker ps to see which port 8080 in the container is mapped to the local port
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES075543f8f5b6 spring-boot-docker-start "java -jar /spring..." About a minute ago Up About a minute 0.0.0.0:32768->8080/tcp trusting_noether
It is sure to be port 32768.
Step 4: Enter http://localhost:32768/hello in the browser and see the results
This means that we have successfully accessed the program in the container!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.