Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize web Engineering docker based on maven

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to achieve web engineering docker based on maven". In daily operation, I believe many people have doubts about how to achieve web engineering docker based on maven. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to achieve web engineering docker based on maven". Next, please follow the editor to study!

An example of web Engineering docker based on maven

This example is used to demonstrate the practice of introducing docker into the development process of a web project. It mainly includes four parts:

Write servlet with java and return HelleWorld

Configure web.xml

Write Dockerfile

Use maven as the build tool and use dockerfile-maven-plugin to build docker images (https://github.com/spotify/dockerfile-maven)

HelloWorldpackage com.jolly.backend;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class Hello extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter (). Println ("HelloWorld!");} web.xml helloServlet com.jolly.backend.Hello helloServlet / dockerfile

Use the tomcat:8-jdk8-slim image as the base image (slim means "slim", meaning compact, so that the size of the image will be much smaller), but the default setting of slim does not automatically decompress the war.

FROM tomcat:8-jdk8ARG WAR_FILECOPY target/$ {WAR_FILE} / usr/local/tomcat/webapps/ROOT.war

Step on the pit:

If you do not specify tag, lastest will be used, which may cause incompatibility problems. For example:

Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException

Use tomcat's slim package to reduce the size of the mirror, try to use the tomcat:8-jdk8-slim image as the base image (slim means "slim", which means a much smaller size), but the corresponding features will be much less.

Construction of maven project

The project defines a web project and introduces the dockerfile-maven-plugin plug-in. The pom file is defined as follows:

4.0.0 com.jolly.backend helloDocker 1.0-SNAPSHOT war javax.servlet javax.servlet-api 3.0.1 provided com.spotify dockerfile-maven-plugin 1.4.13 Default build push docker-hub.jc.com/$ { Project.artifactId} ${project.version} ${project.build.finalName} .war execute the build

After the above coding is completed, you can build the image:

$mvn package$ docker images-filter=reference='localhost/*:*'REPOSITORY TAG IMAGE ID CREATED SIZElocalhost/hello-docker 1.0-SNAPSHOT b6ee78dde85a About a minute ago 649MB

Publish the image to the repository:

Mvn dockerfile:push at this point, the study on "how to implement maven-based web engineering docker" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report