在Docker中有两种方式来安装tomcat服务器。

1、从Docker Hub中下载Tomcat镜像

搜索Docker Hub中的Tomcat镜像

docker search tomcat

排名靠前的是官方的Tocmat镜像,来自官方镜像说明可以参考点击打开链接

从以上列表中选择一个合适的Tomcat下载安装

docker pull tomcat:7 

漫长地下载等待后就下载安装成功了

查看本地镜像

docker images #所有镜像
docker image tomcat:7  #查看REPOSITORY为tomcat:7的镜像

运行Tomcat镜像

docker run -d -p 8081:8080 tomcat:7 


查看运行中的容器

docker ps

停止运行容器

docker stop 运行中容器id

删除容器

docker rm 容器id

删除镜像

docker rmi 镜像id

2、使用dockerfile构建Tomcat容器镜像

很多生产环境下要使用个性化配置的Tomcat,使用dockerfile就是最合适的了

准备好jdk、tomcat安装包、Dockerfile文件、tomcat-users.xml文件

Dockerfile文件

FROM ubuntu:14.04
MAINTAINER ycx <ycx901221@126.com> 
# now add java and tomcat support in the container 
ADD jdk-8u161-linux-x64.tar.gz /usr/local/ 
ADD apache-tomcat-7.0.85.tar.gz /usr/local/
# configuration of java and tomcat ENV 
ENV JAVA_HOME /usr/local/jdk1.8.0_161 
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 
ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.85 
ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.85 
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin 
# container listener port 
EXPOSE 8080 
# startup web application services by self
CMD /usr/local/apache-tomcat-7.0.85/bin/catalina.sh run

tomcat-users.xml文件

<tomcat-users xmlns="http://tomcat.apache.org/xml"  
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"  
              version="1.0">  
<role rolename="manager"/>  
<role rolename="manager-gui"/>  
<role rolename="admin"/>  
<role rolename="admin-gui"/>  
<role rolename="manager-script"/>  
<user username="admin" password="admin" roles="admin-gui,admin,manager-gui,manager,manager-script"/>  
</tomcat-users>

使用ftp将这4个文件上传到Ubuntu服务器

解压apache-tomcat-7.0.85.tar.gz

tar -zxvf apache-tomcat-7.0.85.tar.gz

修改它里面的context.xml、tomcat-users.xml文件

context.xml修改如下

<?xml version="1.0" encoding="UTF-8"?>  
<Context antiResourceLocking="false" privileged="true" docBase="${catalina.home}/webapps/manager" >  
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"    allow="^.*$" />  
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>  
</Context> 

把目录中的tomcat-users.xml覆盖到tomcat中的tomcat-users.xml文件中

tomcat-users.xml如下

<?xml version="1.0" encoding="UTF-8"?>  
<Context antiResourceLocking="false" privileged="true" docBase="${catalina.home}/webapps/manager" >  
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"    allow="^.*$" />  
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>  
</Context> 

打包apache-tomcat-7.0.85.为apache-tomcat-7.0.85.tar.gz

tar czvf apache-tomcat-7.0.85.tar.gz apache-tomcat-7.0.85

在这4个文件的当前目录,执行编译命令,“.”表示当前文件夹

$ sudo docker build -t ycx/tomcat-im .

控制台打印如下,表示编译成功

ubuntu@ubuntu:~/winshare/tomcatweb$ sudo docker build -t ycx/tomcat-im .
Sending build context to Docker daemon  198.8MB
Step 1/12 : FROM ubuntu:14.04
14.04: Pulling from library/ubuntu
c2c80a08aa8c: Pull complete 
6ace04d7a4a2: Pull complete 
f03114bcfb25: Pull complete 
99df43987812: Pull complete 
9c646cd4d155: Pull complete 
Digest: sha256:b92dc7814b2656da61a52a50020443223445fdc2caf1ea0c51fa38381d5608ad
Status: Downloaded newer image for ubuntu:14.04
 ---> 3b853789146f
Step 2/12 : MAINTAINER ycx <ycx901221@126.com>
 ---> Running in 4bddec2f502e
Removing intermediate container 4bddec2f502e
 ---> d93a0f2ccda9
Step 3/12 : ADD jdk-8u161-linux-x64.tar.gz /usr/local/
 ---> 91b07a36381f
Step 4/12 : ADD apache-tomcat-7.0.85.tar.gz /usr/local/
 ---> 7fc87249584c
Step 5/12 : ADD tomcat-users.xml /usr/local/apache-tomcat-7.0.85/conf/tomcat-users.xml
 ---> 7e9c3e5ff344
Step 6/12 : ENV JAVA_HOME /usr/local/jdk1.8.0_161
 ---> Running in bc7e83399dd6
Removing intermediate container bc7e83399dd6
 ---> f8323859c84f
Step 7/12 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in bb82cd2bf8b0
Removing intermediate container bb82cd2bf8b0
 ---> d20b0c25df42
Step 8/12 : ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.85
 ---> Running in 8e50eac104de
Removing intermediate container 8e50eac104de
 ---> f8776a09b35f
Step 9/12 : ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.85
 ---> Running in 7939810f6002
Removing intermediate container 7939810f6002
 ---> 6ae9de1087a6
Step 10/12 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in 79a158e874cc
Removing intermediate container 79a158e874cc
 ---> a41575ff32d4
Step 11/12 : EXPOSE 8080
 ---> Running in 0e63db3d01ba
Removing intermediate container 0e63db3d01ba
 ---> d11af7f6314d
Step 12/12 : CMD /usr/local/apache-tomcat-7.0.85/bin/catalina.sh run
 ---> Running in e620e59127fa
Removing intermediate container e620e59127fa
 ---> a985c7e5dc47
Successfully built a985c7e5dc47
Successfully tagged ycx/tomcat-im:latest

接下来启动Tomcat镜像

$ sudo docker run -d -p 8082:8080 ycx/tomcat-im:latest


其他Docker操作命令如上,就不再重述

自动部署War包到Tomcat镜像

项目pom.xml配置

<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 远程部署,当使用本地调试时禁用 -->
					<url>http://192.168.1.111:8082/manager/text</url>
					<!-- Tomcat的tomcat-users.xml中指定的用户名和密码 -->
					<username>admin</username>
					<password>admin</password>
					<!-- 服务器的名称,与maven全局setting.xml中server的id一致 -->
					<server>tomcat7</server>
					<port>8082</port>
					<path>/</path>
					<!-- 本地调试用下面的 -->
					<!-- <port>8081</port> -->
					<!-- <path>/</path> -->
				</configuration>
            </plugin> 

maven部署命令

clean tomcat7:deploy

eclipse控制台有如下显示表示远程部署成功

[INFO] WEB-INF\web.xml already added, skipping
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:deploy (default-cli) < package @ im-manager-web <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:deploy (default-cli) @ im-manager-web ---
[INFO] Deploying war to http://192.168.1.111:8082/  
Uploading: http://192.168.1.111:8082/manager/text/deploy?path=%2F
Uploaded: http://192.168.1.111:8082/manager/text/deploy?path=%2F (31105 KB at 2066.2 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] im-manager ......................................... SUCCESS [  1.525 s]
[INFO] im-manager-pojo .................................... SUCCESS [  5.164 s]
[INFO] im-manager-dao ..................................... SUCCESS [  2.088 s]
[INFO] im-manager-service ................................. SUCCESS [  2.071 s]
[INFO] im-manager-web ..................................... SUCCESS [01:26 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:38 min
[INFO] Finished at: 2018-04-15T12:48:33+08:00
[INFO] Final Memory: 19M/148M
[INFO] ------------------------------------------------------------------------

然后打开浏览器输入http://192.168.1.111:8082/loginpage,打开项目页面


当然也可以打包成war包单独上传tomcat部署,就不再讲述,希望对大家由帮助

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐