实战Tomcat镜像
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<lwenwu1999@qq.com>
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<lwenwu1999@qq.com>
---> 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) <security@centos.org>"
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>
失败
分析: