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

Implementation of constructing Maven+Tomcat basic Image by Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

In Java programming, most applications are built on Maven, and most of the delivered results are in the form of Tomcat war packages. Therefore, it is necessary to build a basic image based on Maven and Tomcat, which can not only help us improve the efficiency of independent experimental research and analysis, but also reduce the complexity of writing Dockerfile and improve the overall project delivery efficiency to a certain extent.

1. Create a compilation directory

Mkdir-p build_dockercd build_dockervim Dockerfile

two。 Write Dockerfile

First of all, we select the officially maintained maven:3.3.3 image as the base image, and then add Tomcat support on this basis.

FROM maven:3.3.3

If you like the speed of the domestic warehouse, you can also choose Ali's maven:3-jdk-8.

FROM registry.cn-hangzhou.aliyuncs.com/acs/maven:3-jdk-8

Secondly, set the environment variable related to Tomcat and add it to the system PATH variable so that the startup script of Tomcat can be accessed directly in Shell.

ENV CATALINA_HOME/ usr/local/tomcatENV PATH $CATALINA_HOME/bin:$PATHRUN mkdir-p "$CATALINA_HOME" WORKDIR $CATALINA_HOME

Third, add Tomcat GPG-KEY to verify whether the file is correct after Tomcat downloads. The following keyid data is from the official Tomcat-8.

RUN gpg-keyserver pool.sks-keyservers.net-recv-keys\ F22C4FED\ 86867BA6\ E86E29AC\ 307A10A5\ 564C17A3\ 0x7C037D42\ 0BECE548\ 5E763BEC\ 2F6059E7\ 288584E7\ 4B6FAEFB\ 286BACF1\ 731FABEE\ 461B342D\ 0D498E23\ DC3D1B18\ D63011C7\ 30480593

Fourth, set the Tomcat version variable, which can be passed in during construction to change the Tomcat version. Because the Java version on which maven:3.3.3 mirrors depend is 1.8, our Tomcat version also chooses 8.x. Maintaining compilation consistency can maximize the performance of Tomcat.

Here we choose the latest version: 8.5.45

Then use curl to perform the download, verify it, extract it, and delete the redundant bat scripts. (this script is for Windows environments only and is useless in Linux/Mac images)

ENV TOMCAT_VERSION 8.5.45ENV TOMCAT_TGZ_URL https://www.apache.org/dist/tomcat/tomcat-8/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gzRUN set-x\ & & curl-fSL "$TOMCAT_TGZ_URL"-o tomcat.tar.gz\ & & curl-fSL "$TOMCAT_TGZ_URL.asc"-o tomcat.tar.gz.asc\ & & gpg-- verify tomcat.tar.gz.asc\ & & tar -xvf tomcat.tar.gz-- strip-components=1\ & & rm bin/*.bat\ & & rm tomcat.tar.gz*

Fifth, expose the default port 8080 of Tomcat and specify the script that will be executed when the container based on the image starts, which is the Tomcat startup script.

EXPOSE 8080CMD ["catalina.sh", "run"]

3. Build an image

Docker build-t base-maven-tomcat.

That's it. Done.

Attached: complete Dockerfile file

FROM maven:3.3.3ENV CATALINA_HOME/ usr/local/tomcatENV PATH $CATALINA_HOME/bin:$PATHRUN mkdir-p "$CATALINA_HOME" WORKDIR $CATALINA_HOMERUN gpg-- keyserver pool.sks-keyservers.net-- recv-keys\ F22C4FED\ 86867BA6\ E86E29AC\ 307A10A5\ 564C17A3\ 0x7C037D42\ 0BECE548\ 5E763BEC\ 2F6059E7\ 288584E7\ 4B6FAEFB\ 286BACF1\ 731FABEE\ 461B342D\ 0D498E23\ DC3D1B18\ D63011C7\ 30480593ENV TOMCAT_VERSION 8.5.45ENV TOMCAT_TGZ_URL https://www.apache.org/dist/tomcat/tomcat-8/ V$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gzRUN set-x\ & & curl-fSL "$TOMCAT_TGZ_URL"-o tomcat.tar.gz\ & & curl-fSL "$TOMCAT_TGZ_URL.asc"-o tomcat.tar.gz.asc\ & & gpg-- verify tomcat.tar.gz.asc\ & & tar-xvf tomcat.tar.gz-- strip-components=1\ & rm bin/*.bat\ & & rm tomcat.tar.gz*EXPOSE 8080CMD ["catalina.sh" "run"]

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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

Servers

Wechat

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

12
Report