基于tomcat官方镜像构建定制化的tomcat镜像

落日映苍穹つ 2022-03-02 04:28 443阅读 0赞

#tomcker 镜像构建

##为什么要构建tomcat镜像?

  1. 虽然官方已经有了tomcat镜像,但是官方镜像的系统时间是UTC(协调世界时),而我们常用的是CST(北京时间)
  2. 如果我们使用jenkins来进行集成开发时,会发现无法将项目部署到官方的tomcat容器中,这是由于官方tomcat中设置了manager的访问ip限制(默认为本地访问),其他ip无法访问,所以需要修改配置来支持s
    参考文档:tomcat开启远程管理Manager
  3. 部署完成的tomcat需要添加登录限制,不允许未经许可的人员登录 manager页面,这部分通过重新构建容器来限制

拉取tomcat镜像

使用alpine构建的官方tomcat镜像,比其他镜像小很多,便于部署。当然也可以在构建镜像时自动下载

  1. docker pull tomcat:8.5.38-jre8-alpine

添加Tomcat配置文件

1.tomcat-users.xml

配置用户登录账户以及权限

  1. vi tomcat-users.xml

文件内容:

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <tomcat-users>
  3. <role rolename="manager-gui"/>
  4. <role rolename="manager-status"/>
  5. <role rolename="manager-script"/>
  6. <role rolename="admin-gui"/>
  7. <user username="admin" password="admin" roles="manager-gui,manager-status,manager-script,admin-gui" />
  8. </tomcat-users>

2.context.xml

放开ip限制,运行所有地址访问

  1. vi context.xml

文件内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
  3. <Context antiResourceLocking="false" privileged="true" >
  4. <!-- 运行任何ip访问 -->
  5. <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
  6. <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
  7. </Context>

3.dockerfile

创建文件,文件不加扩展名,文件存储路径没有要求,文件名没有要求:

  1. vi dockerfile

文件内容:

  1. FROM tomcat:8.5.38-jre8-alpine
  2. MAINTAINER "制作人自定义"
  3. #定义环境变量
  4. ENV TIME_ZONE Asia/Shanghai
  5. #Alpine目录并无timezone及locatime配置,所以需要先安装
  6. #dockerfile增加命令
  7. RUN \
  8. #安装tzdata安装包
  9. apk add --no-cache tzdata \
  10. #设置时区
  11. && echo "${TIME_ZONE}" > /etc/timezone \
  12. && ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime
  13. # 设置允许所有ip访问
  14. ADD context.xml /usr/local/tomcat/webapps/manager/META-INF/
  15. # 配置登录用户以及权限
  16. ADD tomcat-users.xml /usr/local/tomcat/conf/
  17. RUN rm -rf /usr/local/tomcat/webapps/docs
  18. RUN rm -rf /usr/local/tomcat/webapps/examples

时区设置参考文档:解决Docker容器时区及时间不同步问题
ps:文档中的北京时区应该为Asia,错误写成了Asiz,希望同仁不要再踩坑

创建tomcat镜像

创建容器名称为 mytomcat,版本为 0.1

  1. docker build -t mytomcat:0.1 . -f dockerfile

测试构建的tomcat容器

时区测试

  1. [root@d69 ~]# docker run -d --name newtomcat -p 8083:8080 mytomcat:0.1
  2. [root@d69 ~]# docker exec -it newtomcat03 /bin/bash
  3. bash-4.4# date
  4. Tue Mar 19 17:04:47 CST 2019

访问 :192.168.0.69:8083/manager 页面,登录时需要密码的,这样用户创建没问题,同时manager正常访问
如果manager 保存403说明 ip限制存在

发表评论

表情:
评论列表 (有 0 条评论,443人围观)

还没有评论,来说两句吧...

相关阅读