- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 669字
- 2021-07-02 19:38:27
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:
- 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
- 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"]
- 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 Ctrl + M 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 "====================================================================="
- 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
- Open up a Docker terminal, and go to the folder where these files are located.
- 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 .
- Execute this command, and make sure that an image with the name demo/tomcat is found:
docker images
- 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
- 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
- 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.
推薦閱讀
- 工程軟件開發(fā)技術(shù)基礎(chǔ)
- The React Workshop
- Implementing Cisco Networking Solutions
- 零基礎(chǔ)入門學(xué)習(xí)Python
- Learning Concurrent Programming in Scala
- Clojure for Machine Learning
- Image Processing with ImageJ
- Kubernetes進階實戰(zhàn)
- Webpack實戰(zhàn):入門、進階與調(diào)優(yōu)(第2版)
- Flask Web開發(fā):基于Python的Web應(yīng)用開發(fā)實戰(zhàn)(第2版)
- 零基礎(chǔ)C#學(xué)習(xí)筆記
- Learning Unreal Engine Game Development
- ASP.NET 3.5系統(tǒng)開發(fā)精髓
- jQuery權(quán)威指南
- C++游戲設(shè)計案例教程