一般都是docker安装tomcat,这里介绍下用docker-compose安装tomcat,用容器编排的方式更有助于容器的后期参数修改和维护,docker和docker-compose的安装请查阅docker区的相关文章。
目录
1、在docker仓库下载tomcat9.0
搜索tomcat
[root@10 docker]# docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 2471 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 66 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK] bitnami/tomcat Bitnami Tomcat Docker Image 29 [OK] kubeguide/tomcat-app Tomcat image for Chapter 1 26 consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK] cloudesire/tomcat Tomcat server, 6/7/8 15 [OK] tutum/tomcat Base docker image to run a Tomcat applicatio… 11 aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 11 [OK] arm32v7/tomcat Apache Tomcat is an open source implementati… 6 rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 5 [OK] maluuba/tomcat7-java8 Tomcat7 with java8. 3 arm64v8/tomcat Apache Tomcat is an open source implementati… 2 amd64/tomcat Apache Tomcat is an open source implementati… 2 ppc64le/tomcat Apache Tomcat is an open source implementati… 1 99taxis/tomcat7 Tomcat7 1 [OK] camptocamp/tomcat-logback Docker image for tomcat with logback integra… 1 [OK] i386/tomcat Apache Tomcat is an open source implementati… 1 s390x/tomcat Apache Tomcat is an open source implementati… 0 secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK] picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAGER… 0 [OK] oobsri/tomcat8 Testing CI Jobs with different names. 0 cfje/tomcat-resource Tomcat Concourse Resource 0 jelastic/tomcat An image of the Tomcat Java application serv… 0 appsvc/tomcat 0
下载tomcat
docker pull tomcat:9.0
查看镜像
[root@10 docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 9.0 abc518cb10af 8 days ago 624MB
2、编写docker-compose文件
创建目录 /usr/docker,在该目录下新建docker-compose.yml文件
version: '3' services: tomcat: image: tomcat:9.0 container_name: tomcat9.0 volumes: - tomcat9_data:/usr/local/tomcat restart: always ports: - 8080:8080 volumes: tomcat9_data:
3、使用docker-compose命令创建并启动容器
[root@10 tomcat9]# docker-compose up -d Creating network "tomcat9_default" with the default driver Creating volume "tomcat9_tomcat9_data" with default driver Creating tomcat9.0 ... done
4、访问tomcat端口是否正常
在浏览器输入http://你的ip:8080
5、为tomcat添加用户和权限
进入tomcat容器
[root@10 tomcat9]# docker exec -it tomcat9.0 bash root@b15d0ebe0165:/usr/local/tomcat# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work root@b15d0ebe0165:/usr/local/tomcat# cd conf root@b15d0ebe0165:/usr/local/tomcat/conf# ls Catalina catalina.policy catalina.properties context.xml jaspic-providers.xml jaspic-providers.xsd logging.properties server.xml tomcat-users.xml tomcat-users.xsd web.xml
修改tomcat-users.xml文件,在tomcat-users标签中添加如下内容
<role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <role rolename="admin-script"/> <role rolename="admin-gui"/> <user username="admin" password="123456" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
6、去除tomcat默认只有本机IP访问管理界面的限制
tomcat出于安全考虑默认只有本机才可以访问管理界面,当然也可以更换指定的ip,也可以为了方便直接去除这个限制
修改/webapps/manager/META-INF/content.xml文件,注释掉图中那段代码
7、重新启动容器并访问管理界面
[root@10 tomcat9]# docker-compose restart Restarting tomcat9.0 ... done
进入8080界面后,点击manager-app出现如下图,说明用户就配好了,否则的话会提示403无权限。
tomcat9.0就已经可以正常使用了,docker的便利性无处不在哈哈