Implementation of Docker building Maven+Tomcat basic image

Implementation of Docker building Maven+Tomcat basic image

Preface

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

1. Create a compilation directory

mkdir -p build_docker
cd build_docker
vim Dockerfile

2. Write Dockerfile

First, 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 domestic warehouses, you can also choose Alibaba's maven:3-jdk-8.

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

Secondly, set Tomcat-related environment variables and add them to the system PATH variable so that Tomcat's startup script can be directly accessed in the 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, which is used to verify whether the file is correct after Tomcat downloads it. The following keyid data comes 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. You can pass in the corresponding parameters to change the Tomcat version during the build. Because the Java version that the maven:3.3.3 image depends on is 1.8, we also choose the 8.X version for Tomcat. Maintaining compilation consistency can maximize the performance of Tomcat.

Here we choose the latest version: 8.5.45

Then use curl to download, decompress after verification, and delete the redundant bat script. (This script is only used in Windows environment and is useless in Linux/Mac images)

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 Tomcat's default port 8080 and specify a script to be executed when the container based on this image is started. This script is the Tomcat startup script.

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

3. Build the image

docker build -t base-maven-tomcat .

That's it, done.

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"]

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker builds jenkins+maven code building and deployment platform
  • Implementation steps of Maven plugin to build Docker image
  • How to use Maven plugin to build images in Docker
  • A brief discussion on how to build Docker images using Maven plugins
  • Build Maven projects faster in Docker

<<:  How to integrate the graphic verification code component into the Ant Design Pro login function

>>:  MYSQL implements ranking and querying specified user ranking function (parallel ranking function) example code

Recommend

Solve the matching problem in CSS

Problem Description As we all know, when writing ...

CentOS 6.5 i386 installation MySQL 5.7.18 detailed tutorial

Most people compile MySQL and put it in the syste...

jQuery implements font size adjustment case

This article shares the specific code of jQuery t...

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...

Analysis of basic usage of ul and li

Navigation, small amount of data table, centered &...

Detailed explanation of unique constraints and NULL in MySQL

Preface A requirement I had previously made, to s...

Detailed explanation of Vue's list rendering

Table of contents 1. v-for: traverse array conten...

VMware Workstation virtual machine installation operation method

Virtual machines are very convenient testing soft...

Installation and configuration of mysql 8.0.15 under Centos7

This article shares with you the installation and...

Nginx server https configuration method example

Linux: Linux version 3.10.0-123.9.3.el7.x86_64 Ng...

Design of pop-up windows and floating layers in web design

In the trend of gradual transition from tradition...

Docker exposes port 2375, causing server attacks and solutions

I believe that students who have learned about th...

How to use the WeChat Mini Program lottery component

It is provided in the form of WeChat components. ...