實戰Tomcat映象
阿新 • • 發佈:2020-11-25
TomCat目錄結構說明
#TomCat目錄結構說明: bin:該目錄存放TomCat二進位制可執行檔案,常用的有startup.bat和shutdown.bat檔案,startup.bat用來啟動Tomcat,shutdown.bat用來停止Tomcat; conf:TomCat伺服器的配置目錄,主要有server.xml(配置伺服器資訊,如修改埠號,新增虛擬主機等)、tomcat-users.xml(TomCat使用者與角色資訊,對TomCat後臺管理)和web.xml(Web專案部署描述符檔案); lib:Tomcat所需的jar包; logs:存放TomCat的日誌檔案; temp:存放Tomcat的臨時檔案; webapps:存放所部署的Web專案; work:存放Web專案部署執行時生成的檔案,如java和class檔案; RUNNING.txt:可檢視如何配置TomCat環境變數。 #TomCat配置環境變數 Tomcat是Java應用程式,不直接使用環境變數。 環境變數由Tomcat啟動指令碼使用。指令碼使用 環境變數來準備啟動Tomcat的命令。 (3.1)設定CATALINA_HOME(必填)和CATALINA_BASE(可選) CATALINA_HOME環境變數應設定為 Tomcat“二進位制”分發的根目錄。 Tomcat啟動指令碼具有一些邏輯來設定此變數 如果不存在,則根據啟動指令碼的位置自動進行 在* nix中和Windows的當前目錄中。該邏輯可能不起作用在所有情況下,建議顯式設定變數。 CATALINA_BASE環境變數指定根的位置 Tomcat“活動配置”的目錄。它是可選的。它預設等於CATALINA_HOME。
#jdk目錄結構說明 bin目錄:Java工具的可執行檔案,包括: java、Java編譯器javac、反編譯.class檔案javap、金鑰管理工具keytool、Java文件工具javadoc等。 COPYRIGHT檔案:版權資訊。 db目錄:Java實現的資料庫。 include目錄:.h標頭檔案,C語言開發時用到的標頭檔案。比如jni.h是開發jni程式時必須引用的標頭檔案。 lib目錄: Java類庫,我們經常看到的dt.jar和tools.jar就在這個目錄下。 src.zip檔案:Java類庫原始碼,包括了rt.jar庫中的關鍵部分;除了Java類庫,還包含了啟動器(launcher)的原始碼(C語言實現)。 jre目錄:Java執行環境。 #jdk配置環境變數 JDK安裝完成後,首先要配置JAVA_HOME變數,JAVA_HOME變數指向JDK的安裝目錄,配置JAVA_HOME主要目的是: (1)配置其它JDK環境變數時,可以方便地引用JDK的安裝目錄。 (2)JDK安裝完成後,需要配置Path環境變數,以方便開發者執行Java編譯器等程式。開發者不管是用整合開發工具還是文字編輯工具編寫Java程式時,當整合開發工具呼叫Java編譯器或使用者在Windows 命令列視窗輸入命令執行Java編譯器時,作業系統需要從Path環境變數獲取Java編譯器等程式所在目錄並啟動執行。例如:假設Java整合開發工具或Windows 命令列視窗的當前工作目錄和Java編譯器所在目錄不同,開發者又沒有在path環境變數中配置Java編譯器所在目錄的路徑,作業系統就找不到Java編譯器程式,也就無法啟動Java編譯器程式進行編譯工作。 (3)Java原始碼被被編譯後形成副檔名為“.class”的檔案,JVM(Java虛擬機器)執行Java 程式時,需要載入已被編譯的“.class”的檔案以及該“.class”檔案匯入的其它類(如Java的System類)。設定CLASSPATH的目的就是讓JVM能夠通過CLASSPATH設定的路徑找到這些類檔案 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 告訴jvm要使用或執行的class放在什麼路徑上,便於JVM載入class檔案,.;表示當前路徑,tools.jar和dt.jar為類庫路徑。 CLASSPATH詳解: tools.jar 工具類庫(編譯和執行等),它跟我們程式中用到的基礎類庫沒有關係。我們注意到在Path中變數值bin目錄下的各個exe工具的大小都很小,一般都在27KB左右,這是因為它們實際上僅僅相當於是一層程式碼的包裝,這些工具的實現所要用到的類庫都在tools.jar中,用壓縮軟體開啟tools.jar,你會發現有很多檔案是和bin目錄下的exe工具相對性的,檢視圖一。當然,如果tools.jar的功能只有這些的話,那麼我們根本不用把它加入到CLASSPATH變數中,因為bin目錄下的工具自己可以完成對這些類庫的呼叫,因此tools.jar應該還有其他的功能。在裡面還可以看到有Applet和RMI等相關的檔案,因此tools.jar應該還是遠端呼叫等必須的jar包。tools.jar的其他作用可以檢視其他資料。 dt.jar 執行環境類庫,主要是Swing包,這一點通過用壓縮軟體開啟dt.jar也可以看到。如果在開發時候沒有用到Swing包,那麼可以不用將dt.jar新增到CLASSPATH變數中。 CLASSPATH中的類庫是由Application ClassLoader或者我們自定義的類載入器來載入的,這裡當然不能包括基礎類庫,如果包括基礎類庫的話,並用兩個不同的自定義類載入器去載入該基礎類,那它得到的該基礎類就不是唯一的了,這樣便不能保證Java類的安全性。 基本類庫和擴充套件類庫rt.jar 基本類庫是所有的 import java.* 開頭的類,在 %JAVA_HOME%\jre\lib 目錄下(如其中的 rt.jar、resource.jar ),類載入機制提到,該目錄下的類會由 Bootstrap ClassLoader 自動載入,並通過親委派模型保證了基礎類庫只會被Bootstrap ClassLoader載入,這也就保證了基礎類的唯一性。 擴充套件類庫是所有的 import javax.* 開頭的類,在 %JAVA_HOME%\jre\lib\ext 目錄下,該目錄下的類是由Extension ClassLoader 自動載入,不需要我們指定。
準備映象檔案
準備映象檔案、tomcat壓縮包、jdk壓縮包。
apache-tomcat-9.0.39.tar
jdk-8u271-linux-x64.tar
編寫Dockerfile檔案
官方命名:Dockerfile,build會自動尋找這個檔案,就不需要-f指定了!
容器內部/usr/local目錄下就會看到readme.txt這個檔案
將壓縮包新增進去 跟解壓路徑
注:用ADD命令新增進去的.tar.gz檔案會自動解壓
安裝基礎的命令
設定ENV環境變數 進去的時候就進入到環境變數
配置工作目錄
配置JAVA環境變數
配置Tomcat環境變數
暴露埠
FROM centos MAINTAINER lwenwu<[email protected]> COPY readme.txt /usr/local/readme.txt ADD jdk-8u271-linux-x64.tar.gz /usr/local ADD apache-tomcat-9.0.39.tar.gz /usr/local RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_271 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.39 ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.39 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-9.0.39/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.39/logs/catalina.out
構建映象
docker build -t diytomcat .
構建過程
[root@localhost dockerfile]# docker build -t testtomcat .
Sending build context to Docker daemon 154.4MB
Step 1/15 : FROM centos
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
---> 0d120b6ccaa8
Step 2/15 : MAINTAINER lwenwu<[email protected]>
---> Running in 47b5eb8df66b
Removing intermediate container 47b5eb8df66b
---> 49bcdfea4e32
Step 3/15 : COPY readme.txt /usr/local/readme.txt
---> 2a6be7198e44
Step 4/15 : ADD jdk-8u271-linux-x64.tar.gz /usr/local
---> 88e1646ee66d
Step 5/15 : ADD apache-tomcat-9.0.39.tar.gz /usr/local
---> 259ee757cb6f
Step 6/15 : RUN yum -y install vim
---> Running in 2d1b5f504afa
CentOS-8 - AppStream 3.3 MB/s | 5.8 MB 00:01
CentOS-8 - Base 2.5 MB/s | 2.2 MB 00:00
CentOS-8 - Extras 13 kB/s | 8.1 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k
vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M
vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k
which x86_64 2.21-12.el8 BaseOS 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 31 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 176 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 697 kB/s | 48 kB 00:00
(3/5): which-2.21-12.el8.x86_64.rpm 474 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 1.6 MB/s | 1.4 MB 00:00
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 4.2 MB/s | 6.3 MB 00:01
--------------------------------------------------------------------------------
Total 2.2 MB/s | 7.8 MB 00:03
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream 825 kB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <[email protected]>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : which-2.21-12.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/5
Verifying : which-2.21-12.el8.x86_64 5/5
Installed:
gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-13.el8.x86_64
vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch
which-2.21-12.el8.x86_64
Complete!
Removing intermediate container 2d1b5f504afa
---> 104e56b60aec
Step 7/15 : ENV MYPATH /usr/local
---> Running in 9582f9a31cee
Removing intermediate container 9582f9a31cee
---> dd0e414f6f83
Step 8/15 : WORKDIR $MYPATH
---> Running in 5a1b9e7b62c7
Removing intermediate container 5a1b9e7b62c7
---> 4a2e99caa954
Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_271
---> Running in 4d8ee0c67b7b
Removing intermediate container 4d8ee0c67b7b
---> 8da62fbac351
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in 065821bf046e
Removing intermediate container 065821bf046e
---> ad73e38ee464
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.39
---> Running in 4792108504fd
Removing intermediate container 4792108504fd
---> e5ad1371c5c4
Step 12/15 : ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.39
---> Running in 62dbef974ce0
Removing intermediate container 62dbef974ce0
---> a494ad3353e3
Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Running in 2cc75de94c1e
Removing intermediate container 2cc75de94c1e
---> 06bec94756ec
Step 14/15 : EXPOSE 8080
---> Running in ccbe33eefb43
Removing intermediate container ccbe33eefb43
---> 75e040b2f7c3
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.39/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.39/logs/catalina.out
---> Running in ad622bea20ad
Removing intermediate container ad622bea20ad
---> 13aa57b24d7f
Successfully built 13aa57b24d7f
Successfully tagged testtomcat:latest
檢視映象
docker images
後臺執行容器
-d:後臺執行
掛載目錄
docker run -d -p 9090:8080 --name tomcattest -v /data/tomcat/test:/usr/local/apache-tomcat-9.0.39/webapps/test -v /data/tomcat/logs:/usr/local/apache-tomcat-9.0.39/logs testtomcat
測試掛載
容器內:
容器外:
進入容器
檢視執行容器
docker ps
進入容器
docker exec -it ID /bin/bash
訪問測試
容器外面測試
curl localhost:9090
[root@localhost data]# curl localhost:9090
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Apache Tomcat/9.0.39</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="navigation" class="curved container">
<span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span>
<span id="nav-hosts"><a href="/docs/">Documentation</a></span>
<span id="nav-config"><a href="/docs/config/">Configuration</a></span>
<span id="nav-examples"><a href="/examples/">Examples</a></span>
<span id="nav-wiki"><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></span>
<span id="nav-lists"><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></span>
<span id="nav-help"><a href="https://tomcat.apache.org/findhelp.html">Find Help</a></span>
<br class="separator" />
</div>
<div id="asf-box">
<h1>Apache Tomcat/9.0.39</h1>
</div>
<div id="upper" class="curved container">
<div id="congrats" class="curved container">
<h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>
</div>
<div id="notice">
<img src="tomcat.png" alt="[tomcat logo]" />
<div id="tasks">
<h3>Recommended Reading:</h3>
<h4><a href="/docs/security-howto.html">Security Considerations How-To</a></h4>
<h4><a href="/docs/manager-howto.html">Manager Application How-To</a></h4>
<h4><a href="/docs/cluster-howto.html">Clustering/Session Replication How-To</a></h4>
</div>
</div>
<div id="actions">
<div class="button">
<a class="container shadow" href="/manager/status"><span>Server Status</span></a>
</div>
<div class="button">
<a class="container shadow" href="/manager/html"><span>Manager App</span></a>
</div>
<div class="button">
<a class="container shadow" href="/host-manager/html"><span>Host Manager</span></a>
</div>
</div>
<br class="separator" />
</div>
<div id="middle" class="curved container">
<h3>Developer Quick Start</h3>
<div class="col25">
<div class="container">
<p><a href="/docs/setup.html">Tomcat Setup</a></p>
<p><a href="/docs/appdev/">First Web Application</a></p>
</div>
</div>
<div class="col25">
<div class="container">
<p><a href="/docs/realm-howto.html">Realms & AAA</a></p>
<p><a href="/docs/jndi-datasource-examples-howto.html">JDBC DataSources</a></p>
</div>
</div>
<div class="col25">
<div class="container">
<p><a href="/examples/">Examples</a></p>
</div>
</div>
<div class="col25">
<div class="container">
<p><a href="https://wiki.apache.org/tomcat/Specifications">Servlet Specifications</a></p>
<p><a href="https://wiki.apache.org/tomcat/TomcatVersions">Tomcat Versions</a></p>
</div>
</div>
<br class="separator" />
</div>
<div id="lower">
<div id="low-manage" class="">
<div class="curved container">
<h3>Managing Tomcat</h3>
<p>For security, access to the <a href="/manager/html">manager webapp</a> is restricted.
Users are defined in:</p>
<pre>$CATALINA_HOME/conf/tomcat-users.xml</pre>
<p>In Tomcat 9.0 access to the manager application is split between
different users. <a href="/docs/manager-howto.html">Read more...</a></p>
<br />
<h4><a href="/docs/RELEASE-NOTES.txt">Release Notes</a></h4>
<h4><a href="/docs/changelog.html">Changelog</a></h4>
<h4><a href="https://tomcat.apache.org/migration.html">Migration Guide</a></h4>
<h4><a href="https://tomcat.apache.org/security.html">Security Notices</a></h4>
</div>
</div>
<div id="low-docs" class="">
<div class="curved container">
<h3>Documentation</h3>
<h4><a href="/docs/">Tomcat 9.0 Documentation</a></h4>
<h4><a href="/docs/config/">Tomcat 9.0 Configuration</a></h4>
<h4><a href="https://wiki.apache.org/tomcat/FrontPage">Tomcat Wiki</a></h4>
<p>Find additional important configuration information in:</p>
<pre>$CATALINA_HOME/RUNNING.txt</pre>
<p>Developers may be interested in:</p>
<ul>
<li><a href="https://tomcat.apache.org/bugreport.html">Tomcat 9.0 Bug Database</a></li>
<li><a href="/docs/api/index.html">Tomcat 9.0 JavaDocs</a></li>
<li><a href="https://github.com/apache/tomcat/tree/master">Tomcat 9.0 Git Repository at GitHub</a></li>
</ul>
</div>
</div>
<div id="low-help" class="">
<div class="curved container">
<h3>Getting Help</h3>
<h4><a href="https://tomcat.apache.org/faq/">FAQ</a> and <a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></h4>
<p>The following mailing lists are available:</p>
<ul>
<li id="list-announce"><strong><a href="https://tomcat.apache.org/lists.html#tomcat-announce">tomcat-announce</a><br />
Important announcements, releases, security vulnerability notifications. (Low volume).</strong>
</li>
<li><a href="https://tomcat.apache.org/lists.html#tomcat-users">tomcat-users</a><br />
User support and discussion
</li>
<li><a href="https://tomcat.apache.org/lists.html#taglibs-user">taglibs-user</a><br />
User support and discussion for <a href="https://tomcat.apache.org/taglibs/">Apache Taglibs</a>
</li>
<li><a href="https://tomcat.apache.org/lists.html#tomcat-dev">tomcat-dev</a><br />
Development mailing list, including commit messages
</li>
</ul>
</div>
</div>
<br class="separator" />
</div>
<div id="footer" class="curved container">
<div class="col20">
<div class="container">
<h4>Other Downloads</h4>
<ul>
<li><a href="https://tomcat.apache.org/download-connectors.cgi">Tomcat Connectors</a></li>
<li><a href="https://tomcat.apache.org/download-native.cgi">Tomcat Native</a></li>
<li><a href="https://tomcat.apache.org/taglibs/">Taglibs</a></li>
<li><a href="/docs/deployer-howto.html">Deployer</a></li>
</ul>
</div>
</div>
<div class="col20">
<div class="container">
<h4>Other Documentation</h4>
<ul>
<li><a href="https://tomcat.apache.org/connectors-doc/">Tomcat Connectors</a></li>
<li><a href="https://tomcat.apache.org/connectors-doc/">mod_jk Documentation</a></li>
<li><a href="https://tomcat.apache.org/native-doc/">Tomcat Native</a></li>
<li><a href="/docs/deployer-howto.html">Deployer</a></li>
</ul>
</div>
</div>
<div class="col20">
<div class="container">
<h4>Get Involved</h4>
<ul>
<li><a href="https://tomcat.apache.org/getinvolved.html">Overview</a></li>
<li><a href="https://tomcat.apache.org/source.html">Source Repositories</a></li>
<li><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></li>
<li><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></li>
</ul>
</div>
</div>
<div class="col20">
<div class="container">
<h4>Miscellaneous</h4>
<ul>
<li><a href="https://tomcat.apache.org/contact.html">Contact</a></li>
<li><a href="https://tomcat.apache.org/legal.html">Legal</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
</ul>
</div>
</div>
<div class="col20">
<div class="container">
<h4>Apache Software Foundation</h4>
<ul>
<li><a href="https://tomcat.apache.org/whoweare.html">Who We Are</a></li>
<li><a href="https://tomcat.apache.org/heritage.html">Heritage</a></li>
<li><a href="https://www.apache.org">Apache Home</a></li>
<li><a href="https://tomcat.apache.org/resources.html">Resources</a></li>
</ul>
</div>
</div>
<br class="separator" />
</div>
<p class="copyright">Copyright ©1999-2020 Apache Software Foundation. All Rights Reserved</p>
</div>
</body>
</html>
[root@localhost data]#
網頁測試
http://192.168.100.100:9090
【坑】報錯日誌:
/usr/local/apache-tomcat-9.0.39/bin/catalina.sh: line 502: /usr/local/jdk.1.8.0.27/bin/java: No such file or directory
/usr/local/apache-tomcat-9.0.39/bin/catalina.sh: line 502: /usr/local/jdk.1.8.0.27/bin/java: No such file or directory
分析修改環境變數
#ENV JAVA_HOME /usr/local/jdk1.8.0_271 java環境變數錯誤,導致後面引用的變數都錯誤,最終導致Tomcat沒有啟動成功。
html測試
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello.lwenwu!</title>
</head>
<body>
Hello World!<br/>
<%
out.println("this is a test file");
%>
</body>
</html>
失敗
分析: