Implementation of Docker Building Maven+Tomcat Basic Mirror

  • 2021-06-28 14:39:30
  • OfStack

Preface

In Java programming, most applications are built on Maven, and most of the results delivered are in the form of Tomcat's war package. Therefore, it is necessary to build a basic mirror based on Maven and Tomcat, which can not only help us to improve the efficiency of self-study analysis in peacetime, but also reduce the complexity of writing Dockerfile in a certain degree.Improve overall project delivery efficiency.

1. Create compilation directory


mkdir -p build_docker
cd build_docker
vim Dockerfile

2. Write Dockerfile

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


FROM maven:3.3.3 

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


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

Second, set Tomcat-related environment variables and add them to the system PATH variable so that the Tomcat startup script can be accessed directly in Shell.


ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME

Third, add Tomcat GPG-KEY to verify that 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 so that you can pass in parameters to change the Tomcat version when you build it.Since maven:3.3.3 mirroring relies on version 1.8 of Java, our Tomcat version also chooses version 8.X, which maximizes Tomcat performance by maintaining compile 1 consistency.

Here we choose the latest version: 8.5.45

Then use curl to perform the download, validate the decompression, and delete the extra bat scripts. (This script is for the Windows environment only and is not useful in the Linux/Mac image)


ENV TOMCAT_VERSION 8.5.45
ENV TOMCAT_TGZ_URL https://www.apache.org/dist/tomcat/tomcat-8/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz

RUN 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 a script to execute at startup for the mirror-based container, which is the Tomcat startup script.


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

3. Build a mirror


docker build -t base-maven-tomcat . 

Here it is.

Attachment: Complete Dockerfile file


FROM maven:3.3.3

ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME

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

ENV TOMCAT_VERSION 8.5.45
ENV TOMCAT_TGZ_URL https://www.apache.org/dist/tomcat/tomcat-8/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz

RUN 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 8080
CMD ["catalina.sh", "run"]


Related articles: