前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker采用Dockerfile方式构建Tomcat容器镜像

Docker采用Dockerfile方式构建Tomcat容器镜像

作者头像
拓荒者
发布2019-03-11 11:20:46
1.3K0
发布2019-03-11 11:20:46
举报
文章被收录于专栏:运维经验分享运维经验分享

开启Tomcat远程部署设置

tomcat-users.xml

代码语言:javascript
复制
<role rolename="manager-gui"/> 
<role rolename="manager-script"/> 
<user username="tomcat" password="password" roles="manager-gui, manager-script"/>
  • 1
  • 2
  • 3

Dockerfile配置

文件路径
文件路径
代码语言:javascript
复制
FROM ubuntu:14.04
MAINTAINER boonya <boonya@sina.com> 
# now add java and tomcat support in the container 
ADD jdk-8u121-linux-x64.tar.gz /usr/local/ 
ADD apache-tomcat-8.5.16.tar.gz /usr/local/ 
ADD tomcat-users.xml /usr/local/apache-tomcat-8.5.16/conf/tomcat-users.xml
# configuration of java and tomcat ENV 
ENV JAVA_HOME /usr/local/jdk1.8.0_121 
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.16 
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.16 
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-8.5.16/bin/catalina.sh run
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Dockerfile构建

docker build -t boonya/tomcat-web .

注意:”.”表示当前路径,也可以写Dockerfile的绝对路径。

代码语言:javascript
复制
root@default:/usr/local/docker# docker build -t boonya/tomcat-web .
Sending build context to Docker daemon  192.7MB
Step 1/12 : FROM ubuntu:14.04
 ---> 4a2820e686c4
Step 2/12 : MAINTAINER boonya <boonya@sina.com>
 ---> Using cache
 ---> ffb09e7abb8a
Step 3/12 : ADD jdk-8u121-linux-x64.tar.gz /usr/local/
 ---> 776b93471ed0
Removing intermediate container c1341cc694b6
Step 4/12 : ADD apache-tomcat-8.5.16.tar.gz /usr/local/
 ---> f8bedc6d1f75
Removing intermediate container 49bc9c2a0eb6
Step 5/12 : ADD tomcat-users.xml /usr/local/apache-tomcat-8.5.16/conf/tomcat-users.xml
 ---> b906a8642f1a
Removing intermediate container 28b1399fa84d
Step 6/12 : ENV JAVA_HOME /usr/local/jdk1.8.0_121
 ---> Running in f17b6168acd5
 ---> 8a347fbfc46e
Removing intermediate container f17b6168acd5
Step 7/12 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in b60488fb048f
 ---> dfae16b2b559
Removing intermediate container b60488fb048f
Step 8/12 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.16
 ---> Running in 3b6e4c12391d
 ---> 6e1ce15492c3
Removing intermediate container 3b6e4c12391d
Step 9/12 : ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.16
 ---> Running in d27dbd0eb6ce
 ---> d3ff8cacd1a2
Removing intermediate container d27dbd0eb6ce
Step 10/12 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in ebbbcae14f6e
 ---> a3380e6627cc
Removing intermediate container ebbbcae14f6e
Step 11/12 : EXPOSE 8080
 ---> Running in fcbbdccc369c
 ---> 2985c218b2e6
Removing intermediate container fcbbdccc369c
Step 12/12 : CMD /usr/local/apache-tomcat-8.5.16/bin/catalina.sh run
 ---> Running in 7fba86d7a6dd
 ---> 09677d05b579
Removing intermediate container 7fba86d7a6dd
Successfully built 09677d05b579
Successfully tagged boonya/tomcat-web:latest
root@default:/usr/local/docker# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

启动Tomcat镜像

代码语言:javascript
复制
docker run -p 8080:8080 boonya/tomcat-web:latest
  • 1

启动成功后访问http://host:8080可以看到如下界面:

Tomcat管理页面
Tomcat管理页面

查看是否启动:

代码语言:javascript
复制
docker@default:~$ docker ps 
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
875187ffb37d        boonya/tomcat-web:latest   "/bin/sh -c '/usr/..."   32 minutes ago      Up 32 minutes       0.0.0.0:8080->8080/tcp   cocky_goodall
docker@default:~$ 
  • 1
  • 2
  • 3
  • 4

自动部署到Tomcat镜像配置

maven settings.xml

代码语言:javascript
复制
<servers>
   <server> 
     <id>TomcatServer</id>
     <username>tomcat</username> 
     <password>password</password> 
    </server>
</servers>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

项目 pom.xml配置

代码语言:javascript
复制
<build>
        <finalName>webtest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <url>http://192.168.99.100:8080/manager/text</url>
                    <server>TomcatServer</server>
                    <path>/webtest</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

远程部署403问题:

代码语言:javascript
复制
[INFO] Deploying war to http://192.168.99.100:8080/webtest  
Uploading: http://192.168.99.100:8080/manager/text/deploy?path=%2Fwebtest
2/4 KB   
4/4 KB   
Uploaded: http://192.168.99.100:8080/manager/text/deploy?path=%2Fwebtest (4 KB at 140.3 KB/sec)

[INFO] tomcatManager status code:403, ReasonPhrase:
[INFO] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
[INFO] <html>
[INFO]  <head>
[INFO]   <title>403 Access Denied</title>
[INFO]   <style type="text/css">
[INFO]     <!--
[INFO]     BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;font-size:12px;}
[INFO]     H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
[INFO]     PRE, TT {border: 1px dotted #525D76}
[INFO]     A {color : black;}A.name {color : black;}
[INFO]     -->
[INFO]   </style>
[INFO]  </head>
[INFO]  <body>
[INFO]    <h1>403 Access Denied</h1>
[INFO]    <p>
[INFO]     You are not authorized to view this page.
[INFO]    </p>
[INFO]    <p>
[INFO]     By default the Manager is only accessible from a browser running on the
[INFO]     same machine as Tomcat. If you wish to modify this restriction, you'll need
[INFO]     to edit the Manager's <tt>context.xml</tt> file.
[INFO]    </p>
[INFO]    <p>
[INFO]     If you have already configured the Manager application to allow access and
[INFO]     you have used your browsers back button, used a saved book-mark or similar
[INFO]     then you may have triggered the cross-site request forgery (CSRF) protection
[INFO]     that has been enabled for the HTML interface of the Manager application. You
[INFO]     will need to reset this protection by returning to the
[INFO]     <a href="/manager/html">main Manager page</a>. Once you
[INFO]     return to this page, you will be able to continue using the Manager
[INFO]     application's HTML interface normally. If you continue to see this access
[INFO]     denied message, check that you have the necessary permissions to access this
[INFO]     application.
[INFO]    </p>
[INFO]    <p>
[INFO]     If you have not changed
[INFO]     any configuration files, please examine the file
[INFO]     <tt>conf/tomcat-users.xml</tt> in your installation. That
[INFO]     file must contain the credentials to let you use this webapp.
[INFO]    </p>
[INFO]    <p>
[INFO]     For example, to add the <tt>manager-gui</tt> role to a user named
[INFO]     <tt>tomcat</tt> with a password of <tt>s3cret</tt>, add the following to the
[INFO]     config file listed above.
[INFO]    </p>
[INFO] <pre>
[INFO] &lt;role rolename="manager-gui"/&gt;
[INFO] &lt;user username="tomcat" password="s3cret" roles="manager-gui"/&gt;
[INFO] </pre>
[INFO]    <p>
[INFO]     Note that for Tomcat 7 onwards, the roles required to use the manager
[INFO]     application were changed from the single <tt>manager</tt> role to the
[INFO]     following four roles. You will need to assign the role(s) required for
[INFO]     the functionality you wish to access.
[INFO]    </p>
[INFO]     <ul>
[INFO]       <li><tt>manager-gui</tt> - allows access to the HTML GUI and the status
[INFO]           pages</li>
[INFO]       <li><tt>manager-script</tt> - allows access to the text interface and the
[INFO]           status pages</li>
[INFO]       <li><tt>manager-jmx</tt> - allows access to the JMX proxy and the status
[INFO]           pages</li>
[INFO]       <li><tt>manager-status</tt> - allows access to the status pages only</li>
[INFO]     </ul>
[INFO]    <p>
[INFO]     The HTML interface is protected against CSRF but the text and JMX interfaces
[INFO]     are not. To maintain the CSRF protection:
[INFO]    </p>
[INFO]    <ul>
[INFO]     <li>Users with the <tt>manager-gui</tt> role should not be granted either
[INFO]         the <tt>manager-script</tt> or <tt>manager-jmx</tt> roles.</li>
[INFO]     <li>If the text or jmx interfaces are accessed through a browser (e.g. for
[INFO]         testing since these interfaces are intended for tools not humans) then
[INFO]         the browser must be closed afterwards to terminate the session.</li>
[INFO]    </ul>
[INFO]    <p>
[INFO]     For more information - please see the
[INFO]     <a href="/docs/manager-howto.html">Manager App HOW-TO</a>.
[INFO]    </p>
[INFO]  </body>
[INFO] </html>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.674 s
[INFO] Finished at: 2017-07-07T14:26:34+08:00
[INFO] Final Memory: 26M/204M
[INFO] ------------------------------------------------------------------------
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96

注:远程部署的错误请参考Docker开启Tomcat8远程服务端管理角色权限构建tomcat远程部署镜像

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/08/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 开启Tomcat远程部署设置
  • Dockerfile配置
  • Dockerfile构建
  • 启动Tomcat镜像
  • 自动部署到Tomcat镜像配置
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档