官术网_书友最值得收藏!

Setting up the Tomcat 8.x as a container service

The following steps can be used to set up Tomcat 8.x along with Java 8 and Maven 3.x as one container:

  1. Create a folder, and put the following files within the folder. The source code for the files will be given as follows:
    • tomcat.df
    • create_tomcat_admin_user.sh
    • run.sh
  2. Copy the following source code for tomcat.df:
        FROM phusion/baseimage:0.9.17

        RUN echo "deb http://archive.ubuntu.com/ubuntu trusty main universe" > /etc/apt/sources.list

        RUN apt-get -y update

        RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -q python-software-properties software-properties-common

        ENV JAVA_VER 8
        ENV JAVA_HOME /usr/lib/jvm/java-8-oracle

        RUN echo 'deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main' >> /etc/apt/sources.list && \
         echo 'deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main' >> /etc/apt/sources.list && \
         apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C2518248EEA14886 && \
         apt-get update && \
         echo oracle-java${JAVA_VER}-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections && \
         apt-get install -y --force-yes --no-install-recommends oracle-java${JAVA_VER}-installer oracle-java${JAVA_VER}-set-default && \
         apt-get clean && \
         rm -rf /var/cache/oracle-jdk${JAVA_VER}-installer

        RUN update-java-alternatives -s java-8-oracle

        RUN echo "export JAVA_HOME=/usr/lib/jvm/java-8-oracle" >> ~/.bashrc

        RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
        
        ENV MAVEN_VERSION 3.3.9

        RUN mkdir -p /usr/share/maven \
 && curl -fsSL http://apache.osuosl.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz \
 | tar -xzC /usr/share/maven --strip-components=1 \
 && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

        ENV MAVEN_HOME /usr/share/maven

        VOLUME /root/.m2

        RUN apt-get update && \
         apt-get install -yq --no-install-recommends wget pwgen ca-certificates && \
         apt-get clean && \
         rm -rf /var/lib/apt/lists/*
        ENV TOMCAT_MAJOR_VERSION 8
        ENV TOMCAT_MINOR_VERSION 8.5.8
        ENV CATALINA_HOME /tomcat

        RUN wget -q         https://archive.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR_VERSION}/v${TOMCAT_MINOR_VERSION}/bin/apache-tomcat-${TOMCAT_MINOR_VERSION}.tar.gz && \
 wget -qO- https://archive.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR_VERSION}/v        ${TOMCAT_MINOR_VERSION}/bin/apache-tomcat-${TOMCAT_MINOR_VERSION}.tar.gz.md5 | md5sum 
-c - && \
         tar zxf apache-tomcat-*.tar.gz && \
         rm apache-tomcat-*.tar.gz && \
         mv apache-tomcat* tomcat
        
        ADD create_tomcat_admin_user.sh /create_tomcat_admin_user.sh
        RUN mkdir /etc/service/tomcat
        ADD run.sh /etc/service/tomcat/run
        RUN chmod +x /*.sh
        RUN chmod +x /etc/service/tomcat/run

        EXPOSE 8080

        CMD ["/sbin/my_init"]
  1. Copy the following code in a file named create_tomcat_admin_user.sh. This file should be created in the same folder as the preceding file, tomcat.df. While copying into notepad and later using with the Docker terminal, you may find CtrlM inserted at the end of the line. Make sure that those lines are appropriately handled and removed:
        #!/bin/bash

        if [ -f /.tomcat_admin_created ]; then
         echo "Tomcat 'admin' user already created"
         exit 0
        fi

        PASS=${TOMCAT_PASS:-$(pwgen -s 12 1)}
        _word=$( [ ${TOMCAT_PASS} ] && echo "preset" || echo "random" )

        echo "=> Creating an admin user with a ${_word} password in Tomcat"
        sed -i -r 's/<\/tomcat-users>//' ${CATALINA_HOME}/conf/tomcat-users.xml
        echo '<role rolename="manager-gui"/>' >> ${CATALINA_HOME}/conf/tomcat-users.xml
        echo '<role rolename="manager-script"/>' >> ${CATALINA_HOME}/conf/tomcat-users.xml
        echo '<role rolename="manager-jmx"/>' >> ${CATALINA_HOME}/conf/tomcat-users.xml
        echo '<role rolename="admin-gui"/>' >> ${CATALINA_HOME}/conf/tomcat-users.xml
        echo '<role rolename="admin-script"/>' >> ${CATALINA_HOME}/conf/tomcat-users.xml
        echo "<user username="admin" password="${PASS}" roles="manager-gui,manager-script,manager-jmx,admin-gui, admin-script"/>" >> ${CATALINA_HOME}/conf/tomcat-users.xml
        echo '</tomcat-users>' >> ${CATALINA_HOME}/conf/tomcat-users.xml
        echo "=> Done!"
        touch /.tomcat_admin_created

        echo "===================================================================="
echo "You can now configure to this Tomcat server using:"
echo ""
echo " admin:${PASS}"
echo ""
echo "====================================================================="
  1. Copy the following code in a file named as run.sh in the same folder as the preceding two files:
        #!/bin/bash

        if [ ! -f /.tomcat_admin_created ]; then
         /create_tomcat_admin_user.sh
        fi

        exec ${CATALINA_HOME}/bin/catalina.sh run
  1. Open up a Docker terminal, and go to the folder where these files are located.
  2. Execute the following command to create the Tomcat image. In a few minutes, the Tomcat image will be created:
      docker build -f tomcat.df -t demo/tomcat:8 .
  1. Execute this command, and make sure that an image with the name demo/tomcat is found:
      docker images
  1. Next, run a container with the name tomcatdev using the following command:
      docker run -ti -d -p 8080:8080 --name tomcatdev -v "$PWD":/mnt/   demo/tomcat:8
  1. Open a browser, and type the URL as http://192.168.99.100:8080/. You should be able to see the following page be loaded. Note the URL and the Tomcat version, 8.5.8. This is the same version that we installed earlier (check Figure 1.4):
Figure 1.20: Tomcat 8.5.8 installed as a Docker container
  1. You can access the container through the terminal with the following command. Make sure to check the Tomcat installation inside the folder /tomcat. Also, execute the commands java -version and mvn -v to check the version of Java and Maven respectively:
      docker exec -ti tomcatdev /bin/bash

In this section, you learnt to set up Tomcat 8.5.8 along with Java 8 and Maven 3.x as one container.

主站蜘蛛池模板: 城步| 石家庄市| 长春市| 广宗县| 海兴县| 沾益县| 江川县| 寿宁县| 额济纳旗| 隆德县| 临邑县| 昌图县| 贡觉县| 乐清市| 普定县| 德令哈市| 广德县| 榆林市| 山东省| 宁陕县| 定襄县| 弋阳县| 永城市| 玉龙| 汝州市| 民乐县| 灵武市| 沈阳市| 田东县| 龙里县| 巴东县| 桂林市| 宝清县| 承德县| 长海县| 金昌市| 抚远县| 伽师县| 徐闻县| 农安县| 溆浦县|