这里要注意一下,最新的tomcat版本里webapps下没有
docs examples host-manager manager
- 如果你需要安装host-manager 和 manager 以及 examples 可以选择 8.5.20版本。当前版本里面自带了以上所有。
1. 搜索 tomcat
docker search docker.io/tomcat
2. pull拉取镜像
docker pull docker.io/tomcat:latest
docker pull docker.io/tomcat:8.5.20
3. 直接运行镜像版本如果本地没有会自动拉取,可以忽略2
docker run -p 8080:8080 -itd --name tomcat8.5.20 docker.io/tomcat:8.5.20
- –rm 容器停止的时候自动删除,这里我们不加–rm 参数如果有配置需要修改需要提交容器,则不能让容器在停止的时候删除。
- –name 指定容器的名称
- -p 8080:8080 映射宿主机8080 到容器8080
4. 点击 Manager App
If you have not changed any configuration files, please examine the file conf/tomcat-users.xml in your installation. That file must contain the credentials to let you use this webapp.
For example, to add the manager-gui role to a user named tomcat with a password of s3cret, add the following to the config file listed above.
<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single manager role to the following four roles. You will need to assign the role(s) required for the functionality you wish to access.
manager-gui - allows access to the HTML GUI and the status pages
manager-script - allows access to the text interface and the status pages
manager-jmx - allows access to the JMX proxy and the status pages
manager-status - allows access to the status pages only
The HTML interface is protected against CSRF but the text and JMX interfaces are not. To maintain the CSRF protection:
Users with the manager-gui role should not be granted either the manager-script or manager-jmx roles.
If the text or jmx interfaces are accessed through a browser (e.g. for testing since these interfaces are intended for tools not humans) then the browser must be closed afterwards to terminate the session.
- 此时是不能访问manager的,根据上面的提示在conf的 tomcat-users.xml下配置访问用户
主要要添加manager-gui角色给用户
5. 进入容器修改tomcat-users.xml
docker exec -it tomcat8.5.20 /bin/bash
root@271cae195202:/usr/local/tomcat# vi
bash: vi: command not found
6. 安装vim
# sudo apt-get update
# sudo apt-get install vim
- 如果不是root用户登录安装加sudo提升权限
- 如果是root用户直接 apt-get install vim
7. 回到4 修改tomcat-users.xml
cd conf
vi tomcat-users.xml
- 按 i 键进入编辑模式
- 完成后按Esc退出编辑模式
- 最后按:wq!保存并退出
- 如果你想看自己修改的内容可以通过 cat tomcat-users.xml查看
<?xml version="1.0" encoding="UTF-8"?>
<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-gui"/>
<user username="tomcat" password="s3crettomcat" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
</tomcat-users>
8. 修改manager里的context.xml
- 进入META-INF目录下
cd /usr/local/tomcat/webapps/manager/META-INF
vi context.xml
- 将 allow 修改为
^.*$
allow 是配置允许访问的ip
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="^.*$" />
- 修改war包部署限制(50M), 超过限制会出现org.apache.catalina.filters.CSRF_NONCE的异常 [
upload?org.apache.catalina.filters.CSRF_NONCE=21A70615DEF2FADEAE71471F11807F50
]
进入 /usr/local/tomcat/webapps/manager/WEB-INF
目录下修改web.xml
这里的52428800 = 5010241024
52428800后面加个零 524288000 就是500MB
按ESC退出编辑模式按:wq!保存并退出
9. 提交容器为新的镜像
docker stop tomcat8.5.20
docker commit -a "me" -m "update tomcat-users.xml and allow for manager" tomcat8.5.20 register.me.ltd/tomcat:8.5.20
- -a 表示 author 作者
- -m 表示 注释或者说明或者备注
- register.me.ltd 仓库地址
- tomcat8.5.20 要提交的容器的名称,也可以是容器id
10. 运行新提交的镜像
docker run -p 8080:8080 -itd --name mem-tomcat8.5.20 register.me.ltd/tomcat:8.5.20
It’s good.
11. 部署war包
这里有几种方式,其中最简单的就是在manager界面WAR file to deploy上传war部署:
另一种是容器启动的时候挂载宿主机目录到容器的/usr/local/tomcat/webapps目录
直接将war包丢进宿主机目录即可,还有一种方式是使用docker cp命令将war拷贝到容器的webapps目录
下面举例用挂载Volume启动容器
docker run -v /home/ubuntu/DockerVolumeTomcatWebapps:/usr/local/tomcat/webapps -p 8080:8080 -itd --name tomcat8.5.20 docker.io/tomcat:8.5.20
- /home/ubuntu/DockerVolumeTomcatWebapps 宿主机目录
- 将war包上传到宿主机目录即可