Docker

使用 Centos 7 在 docker 容器中安裝 Tomcat 8.*

  • August 13, 2020

我想創建一個 docker 容器來在其中安裝 Tomcat。看來我的 docker 編譯沒有問題,並且我也能夠執行 tomcat 而沒有通過日誌報告任何問題。但是,如果我嘗試打開 tomcat,我會收到 404 錯誤。我不知道我錯過了什麼。請問有什麼想法嗎?接下來是我的 Dockerfile 的內容:

FROM centos:7
MAINTAINER joseccz

EXPOSE 8080
EXPOSE 8009


RUN yum install -y mc
RUN yum install -y wget
# RUN yum install -y apr-devel openssl-devel
RUN yum install -y gcc
RUN yum install epel-release -y
RUN yum install dnf -y
RUN yum install net-tools -y
RUN yum install libtool -y

ENV JAVA_HOME       /opt/javaSDK
ENV JRE_HOME        ${JAVA_HOME}/jre

ADD javaSDK ${JAVA_HOME}

ADD tomcat.service /etc/systemd/system/




# ----------------------------------------------- Copy Tomcat Files to /opt/tomcat -----------------------------------------------
ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.5.45
ENV CATALINA_HOME   /opt/tomcat

# http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz

ENV PATH        $JAVA_HOME/bin:$CATALINA_HOME/bin:$CATALINA_HOME/lib:$PATH

RUN wget http://www-us.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR}/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz && \
tar -xvf apache-tomcat-${TOMCAT_VERSION}.tar.gz 

RUN mv apache-tomcat-${TOMCAT_VERSION} tomcat

RUN rm apache-tomcat*.tar.gz
RUN mv tomcat /opt/tomcat
# --------------------------------------------------------------------------------------------------------------------------------



RUN groupadd tomcat
RUN useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

# RUN wget https://rpmfind.net/linux/mageia/distrib/cauldron/x86_64/media/core/release/tomcat-native-1.2.23-1.mga8.x86_64.rpm
# RUN rpm -i tomcat-native-1.2.23-1.mga8.x86_64.rpm

# RUN cd /opt/tomcat
RUN chgrp -R tomcat /opt/tomcat/conf
RUN chmod g+rwx /opt/tomcat/conf
RUN chmod g+r /opt/tomcat/conf/*
RUN chown -R tomcat /opt/tomcat/logs/ /opt/tomcat/temp/ /opt/tomcat/webapps/ /opt/tomcat/work/

RUN chgrp -R tomcat /opt/tomcat/bin
RUN chgrp -R tomcat /opt/tomcat/lib
RUN chmod g+rwx /opt/tomcat/bin
RUN chmod g+r /opt/tomcat/bin/*

#----------------------------------Create /opt/software folder for addinitonal soft needed by Tomcat ------------------------------
RUN mkdir /opt/software

#---------------------------------------- copy and compile Development tools ---------------------------------------------------------

RUN yum groupinstall "Development Tools" -y

#---------------------------------------- Donwload and install open ssl ------------------------------------------------------------
RUN wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
RUN tar -xvf openssl-1.0.2l.tar.gz
RUN mv openssl-1.0.2l openssl
RUN mv openssl /opt/software/openssl
RUN cd /opt/software/openssl && ./config -fPIC --prefix=/opt/software/ --openssldir=/opt/software/ && make && make install

#---------------------------------------- Donwload and install open apr ------------------------------------------------------------
ENV APRVERSION 1.6.5
RUN wget http://apache.mirrors.ionfish.org//apr/apr-${APRVERSION}.tar.gz
RUN tar -xvf apr-${APRVERSION}.tar.gz
RUN mv apr-${APRVERSION} apr
RUN mv apr /opt/software/apr
RUN cd /opt/software/apr/ && \
   ./configure --prefix=/opt/software/ && \
    make && make install

#------------------------------------------ Copy TOMCAT native


# compiling tomcat native

RUN cd ${CATALINA_HOME}/bin/ && \
   tar -xvf tomcat-native.tar.gz && \
   cd tomcat-native-1.2.23-src/native/ && \
    ./configure --with-apr=/opt/software/ --with-java-home=/opt/javaSDK/  --with-ssl=/opt/software  --prefix=/opt/tomcat && \
   make && \
   make install

ADD setenv.sh /opt/tomcat/bin/

我的 setenv.sh 文件內容是:

export JAVA_HOME=/opt/javaSDK
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
export LD_LIBRARY_PATH

重要的部分是以某種方式將下一個 xommands 添加到 dockerfile:

#---------------------------------------- Donwload and install open apr ------------------------------------------------------------
ENV APRVERSION 1.6.5
RUN wget http://apache.mirrors.ionfish.org//apr/apr-${APRVERSION}.tar.gz
RUN tar -xvf apr-${APRVERSION}.tar.gz
RUN mv apr-${APRVERSION} apr
RUN mv apr /opt/software/apr
RUN cd /opt/software/apr/ && \
   ./configure --prefix=/opt/software/ && \
    make && make install

#------------------------------------------ Copy TOMCAT native


# compiling tomcat native

RUN cd ${CATALINA_HOME}/bin/ && \
   tar -xvf tomcat-native.tar.gz && \
   cd tomcat-native-1.2.8-src/native/ && \
    ./configure --with-apr=/opt/software/ --with-java-home=/opt/javaSDK/ --with-ssl=/opt/software  --prefix=/opt/tomcat && \
   make && \
   make install

ADD setenv.sh /opt/tomcat/bin/

tomcat-native.tar.gz必須是tomca安裝壓縮文件中包含的要安裝的Tomcat版本中包含的那個。此外,安裝文件中包含的 Tomcat 應用程序範例必須至少手動添加到 Tomcat 的 webapps 文件夾中。我在自動執行此操作時遇到了問題。

引用自:https://serverfault.com/questions/981752