定制tomcat官方镜像,实现时间同步及manager远程访问
2020-04-23 本文已影响0人
沉思的雨季
一、为什么要构建tomcat镜像?
1.官方镜像的系统时间是UTC(协调世界时),而我们常用的是CST(北京时间)。
2.官方tomcat中设置了manager的访问ip限制(默认为本地访问),其他ip无法访问,所以需要修改配置来支持远程管理Manager。
3.tomcat需要添加登录限制,不允许未经许可的人员登录 manager页面,这部分通过重新构建容器来限制。
二、镜像定制化过程
1、拉取tomcat镜像,使用alpine构建的官方tomcat镜像,比其他镜像小很多,便于部署。
docker pull tomcat:8.5.38-jre8-alpine
2、添加Tomcat配置文件tomcat-users.xml,配置用户登录账户以及权限
vi tomcat-users.xml
文件内容:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="manager-script"/>
<role rolename="admin-gui"/>
<user username="admin" password="admin" roles="manager-gui,manager-status,manager-script,admin-gui" />
</tomcat-users>
3.修改context.xml放开ip限制,允许所有地址访问
vi context.xml
文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<Context antiResourceLocking="false" privileged="true" >
<!-- 运行任何ip访问 -->
<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>
4.创建dockerfile文件,文件不加扩展名
vi dockerfile
文件内容:
FROM tomcat:8.5.38-jre8-alpine
MAINTAINER "制作人自定义"
#定义环境变量
ENV TIME_ZONE Asia/Shanghai
#Alpine目录并无timezone及locatime配置,所以需要先安装
#dockerfile增加命令
RUN \
#安装tzdata安装包
apk add --no-cache tzdata \
#设置时区
&& echo "${TIME_ZONE}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime
#设置允许所有ip访问
ADD context.xml /usr/local/tomcat/webapps/manager/META-INF/
#配置登录用户以及权限
ADD tomcat-users.xml /usr/local/tomcat/conf/
RUN rm -rf /usr/local/tomcat/webapps/docs
RUN rm -rf /usr/local/tomcat/webapps/examples
5.创建tomcat镜像,创建容器名称为 mytomcat,版本为 0.1
docker build -t mytomcat:0.1 . -f dockerfile
三、测试构建的tomcat容器
1.时区测试
[root@d69 ~]# docker run -d --name newtomcat -p 8083:8080 mytomcat:0.1
[root@d69 ~]# docker exec -it newtomcat03 /bin/bash
bash-4.4# date
Tue Mar 19 17:04:47 CST 2019
2.访问 :192.168.0.69:8083/manager 页面,登录时需要密码的,这样用户创建没问题,同时manager正常访问
如果manager报错403说明ip限制存在。