maven插件自动部署web应用至Docker容器的tomcat

转自 https://blog.csdn.net/boonya/article/details/74890680

参考:http://blog.51cto.com/diamondlich/1873369

在“Docker采用Dockerfile方式构建Tomcat容器镜像”一文中,Docker镜像的构建是成功了的,但是通过Maven发布项目到Tomcat8却遇到了服务器拒绝访问403的错误,因此才有了本文记述解决Tomcat访问问题的文章。主要是要配置${TOMCAT_HOME}/conf/tomcat-users.xml和${TOMCAT_HOME}/webapps/manager/META-INF/context.xml两个文件。

基础操作说明

因为下载的linux tomcat8没有开放server用户管理和远程权限,所以需要编辑${TOMCAT_HOME}/conf/tomcat-users.xml和${TOMCAT_HOME}/webapps/manager/META-INF/context.xml两个文件。我将所有操作文件都放于/usr/local/docker下:

maven插件自动部署web应用至Docker容器的tomcat

Tomcat 包重新编辑 打包的步骤如下:

1、进入/usr/lcoal/docker解压apache-tomcat-8.5.16.tar.gz

[plain] view plain copy
  1. tar -zxvf apache-tomcat-8.5.16.tar.gz  

2、编辑tomcat-users.xml和context.xml(下面有)

3、重新打包apache-tomcat-8.5.16.tar.gz

[plain] view plain copy
  1. tar czvf apache-tomcat-8.5.16.tar.gz apache-tomcat-8.5.16  

Dockerfile配置(以ubuntu17.10为镜像)

[plain] view plain copy
  1. FROM ubuntu:17.10  
  2. MAINTAINER boonya <[email protected]>   
  3. # now add java and tomcat support in the container   
  4. ADD jdk-8u121-linux-x64.tar.gz /usr/local/   
  5. ADD apache-tomcat-8.5.16.tar.gz /usr/local/   
  6. # ADD tomcat-users.xml /usr/local/apache-tomcat-8.5.16/conf/tomcat-users.xml  
  7. # configuration of java and tomcat ENV   
  8. ENV JAVA_HOME /usr/local/jdk1.8.0_121   
  9. ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar   
  10. ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.16   
  11. ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.16   
  12. ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin   
  13. # container listener port   
  14. EXPOSE 8080   
  15. # startup web application services by self   
  16. CMD /usr/local/apache-tomcat-8.5.16/bin/catalina.sh run  

注:此处注释掉了此前配置的ADD tomcat-users.xml的配置。

Tomcat用户角色配置

编辑${TOMCAT_HOME}/conf/tomcat-users.xml:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <tomcat-users xmlns="http://tomcat.apache.org/xml"  
  4.               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.               xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"  
  6.               version="1.0">  
  7. <role rolename="manager"/>  
  8. <role rolename="manager-gui"/>  
  9. <role rolename="admin"/>  
  10. <role rolename="admin-gui"/>  
  11. <role rolename="manager-script"/>  
  12. <user username="tomcat" password="password" roles="admin-gui,admin,manager-gui,manager,manager-script"/>  
  13. </tomcat-users>  

Manager管理权限过滤配置

编辑${TOMCAT_HOME}/webapps/manager/META-INF/context.xml:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Context antiResourceLocking="false" privileged="true" docBase="${catalina.home}/webapps/manager" >  
  3.   <Valve className="org.apache.catalina.valves.RemoteAddrValve"    allow="^.*$" />  
  4.   <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>  
  5. </Context>  
原始文件只支持本地管理:

[html] view plain copy
  1. <Context antiResourceLocking="false" privileged="true" >  
  2.   <Valve className="org.apache.catalina.valves.RemoteAddrValve"  
  3.          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />  
  4.   <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>  
  5. </Context>  

Docker tomcat镜像构建和运行

以上文件编辑之后就可以开始构建Tomcat的镜像了:

构建:

[plain] view plain copy
  1. docker build -t boonya/tomcat-allow-remote .  

查看镜像:

[plain] view plain copy
  1. boonya/tomcat-allow-remote   latest              6137f64875dd        34 minutes ago      571MB  
  2. boonya/tomcat-web            latest              09677d05b579        2 days ago          571MB  
  3. <none>                       <none>              e1471ab1845a        2 days ago          188MB  
  4. <none>                       <none>              cf240e31edfb        2 days ago          188MB  
  5. <none>                       <none>              717961796ae2        2 days ago          334MB  
  6. apptomcat                    1.0                 249dc3f5dfb0        3 days ago          334MB  
  7. tomcat                       8                   a2fbbcebd67e        8 days ago          334MB  
  8. ubuntu                       14.04               4a2820e686c4        2 weeks ago         188MB  
  9. wurstmeister/kafka           latest              12453f4efa7b        6 weeks ago         265MB  
  10. [email protected]:~$   

运行:
[plain] view plain copy
  1. docker run -p 8080:8080 boonya/tomcat-allow-remote:latest  

验证Tomcat服务器远程管理

进入默认端口主页:

maven插件自动部署web应用至Docker容器的tomcat


验证是否可登录:

maven插件自动部署web应用至Docker容器的tomcat


注:镜像是基于Tomcat8的远程管理角色权限,管理账号和密码是:tomcat/password。

至此,Tomcat server端成功开放管理权限。

验证Maven远程部署项目

maven管理的工程中的pom文件中:


注:如果pom中带s的复数标签已有,只需要将内容添加到标签下即可:

比如:

文件中已经存在plugins标签,往其中添加:


 <plugins>  
            <plugin>
                <groupId>xxxxxxxxxxxxxxxxxx</groupId>  
                <artifactId>xxxxxxxxxxxxxxxxxx</artifactId>  
                <version>xxxxxxxxxxx</version>  
            </plugin>  
         .
         .
         .
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.1</version>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.tomcat.maven</groupId>  
                <artifactId>tomcat7-maven-plugin</artifactId>  
                <version>2.2</version>  
                <configuration>
                    <port>8080</port>   
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/text</url>  
                    <username>admin</username>  
                    <password>adm123</password>  
                    <path>/${project.artifactId}</path>  
                </configuration>  
            </plugin>  
</plugins>


文件中存在properties标签时:


<properties>  
        .
        .
        .
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.deploy>deploy</project.deploy>  
        <project.tomcat.version>8.0.0-RC5</project.tomcat.version>  
</properties>



文件中存在dependencies标签:


 <dependencies>
         <dependency>  
            <groupId>xxxxxxxxxx</groupId>  
            <artifactId>xxxxxxxxxxxx</artifactId>  
            <version>xxxxxxxxxxxxx</version>  
            <scope>xxxxxxxxxxxx</scope>  
        </dependency> 
        .
        .
        .
        <dependency>  
            <groupId>org.apache.tomcat</groupId>  
            <artifactId>tomcat-servlet-api</artifactId>  
            <version>${project.tomcat.version}</version>  
            <scope>provided</scope>  
        </dependency> 
  </dependencies>



pom.xml:

localhost改成你部署的主机的ip

<properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.deploy>deploy</project.deploy>  
        <project.tomcat.version>8.0.0-RC5</project.tomcat.version>  
</properties>  
 <dependencies>
        <dependency>  
            <groupId>org.apache.tomcat</groupId>  
            <artifactId>tomcat-servlet-api</artifactId>  
            <version>${project.tomcat.version}</version>  
            <scope>provided</scope>  
        </dependency> 
  </dependencies>  
  <build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.1</version>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.tomcat.maven</groupId>  
                <artifactId>tomcat7-maven-plugin</artifactId>  
                <version>2.2</version>  
                <configuration>
                    <port>8080</port>   
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/text</url>  
                    <username>admin</username>  
                    <password>adm123</password>  
                    <path>/${project.artifactId}</path>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>
先Maven install安装相关的jar,再Maven build 成功,

最后run as Configuration:

maven插件自动部署web应用至Docker容器的tomcat

实际上的命令是:mvn clean tomcat7:deploy 或 tomcat7:deploy 后台输出日志如下:

[plain] view plain copy
  1. [INFO] Scanning for projects...  
  2. [INFO]   
  3. [INFO] ------------------------------------------------------------------------  
  4. [INFO] Building webtest 0.0.1-SNAPSHOT  
  5. [INFO] ------------------------------------------------------------------------  
  6. [INFO]   
  7. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ webtest ---  
  8. [INFO] Deleting D:\workspace-sts-3.8.4.RELEASE\webtest\target  
  9. [INFO]   
  10. [INFO] >>> tomcat7-maven-plugin:2.1:deploy (default-cli) > package @ webtest >>>  
  11. [INFO]   
  12. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webtest ---  
  13. [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!  
  14. [INFO] Copying 0 resource  
  15. [INFO]   
  16. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ webtest ---  
  17. [INFO] Changes detected - recompiling the module!  
  18. [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!  
  19. [INFO] Compiling 1 source file to D:\workspace-sts-3.8.4.RELEASE\webtest\target\classes  
  20. [INFO]   
  21. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ webtest ---  
  22. [INFO] Not copying test resources  
  23. [INFO]   
  24. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ webtest ---  
  25. [INFO] Not compiling test sources  
  26. [INFO]   
  27. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ webtest ---  
  28. [INFO] Tests are skipped.  
  29. [INFO]   
  30. [INFO] --- maven-war-plugin:2.2:war (default-war) @ webtest ---  
  31. [INFO] Packaging webapp  
  32. [INFO] Assembling webapp [webtest] in [D:\workspace-sts-3.8.4.RELEASE\webtest\target\webtest]  
  33. [INFO] Processing war project  
  34. [INFO] Copying webapp resources [D:\workspace-sts-3.8.4.RELEASE\webtest\src\main\webapp]  
  35. [INFO] Webapp assembled in [243 msecs]  
  36. [INFO] Building war: D:\workspace-sts-3.8.4.RELEASE\webtest\target\webtest.war  
  37. [INFO] WEB-INF\web.xml already added, skipping  
  38. [INFO]   
  39. [INFO] <<< tomcat7-maven-plugin:2.1:deploy (default-cli) < package @ webtest <<<  
  40. [INFO]   
  41. [INFO]   
  42. [INFO] --- tomcat7-maven-plugin:2.1:deploy (default-cli) @ webtest ---  
  43. [INFO] Deploying war to http://192.168.99.100:8080/webtest    
  44. Uploading: http://192.168.99.100:8080/manager/text/deploy?path=%2Fwebtest  
  45. 2/4 KB     
  46. 4/4 KB     
  47. Uploaded: http://192.168.99.100:8080/manager/text/deploy?path=%2Fwebtest (4 KB at 90.8 KB/sec)  
  48.   
  49. [INFO] tomcatManager status code:200, ReasonPhrase:  
  50. [INFO] OK - Deployed application at context path [/webtest]  
  51. [INFO] ------------------------------------------------------------------------  
  52. [INFO] BUILD SUCCESS  
  53. [INFO] ------------------------------------------------------------------------  
  54. [INFO] Total time: 26.821 s  
  55. [INFO] Finished at: 2017-07-09T16:22:11+08:00  
  56. [INFO] Final Memory: 20M/115M  
  57. [INFO] ------------------------------------------------------------------------  
看似成功了,还是要去验证一下。打开服务端管理,看到如下图:


maven插件自动部署web应用至Docker容器的tomcat

webtest项目赫然在列,100%成功了。本文Docker相关的文件可以到我的百度云盘进行下载:http://pan.baidu.com/s/1qXZN4Wc

参考资料

Tomcat8访问管理页面localhost出现:403 Access Denied 

tomcat7, tomcat8的进入manager, tomcat_user.xm配,以及如何配置远程访问manager


tomcat8配置管理员后仍然报403