引言:在centos-7环境里,部署Java web应用到docker里的tomcat上面,但还要连接数据库才行,我们在这里实现把web应用部署到tomcat上面,并跟mysql容器进行连接,才算是真正的部署Java web应用程序。
1、首先必须先在docker里创建一个或两个tomcat容器,把打包后的Java web项目Export-->war文件部署到tomcat容器内部的webapps里面,如你还没有把web项目部署到tomcat容器的webapps里面,怎么能实现tomcat容器跟mysql容器进行连接呢?不会把项目部署到tomcat上的,请参考我上篇博客https://blog.csdn.net/liqz666/article/details/82180581,部署成功后可以看到登陆界面,但登陆界面如果你不连接数据库的话,你登陆之后肯定报404错误。
2、接下使用命令docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 MY在docker里创建一个5.7版本mysql容器,然后进入容器,可以看到你已经成功创建mysql容器, 但是要把数据库何表导入mysql容器才行,最快捷的办法就是实现远程连接navicat工具,但是连接的过程中可能会连接不上,如果连接不上,要么就是权限出了问题,要么就是iptables里的问题,要么就是宿主机端口有问题,如果遇到请看我的另一篇博客:https://blog.csdn.net/liqz666/article/details/82225575,当然没出错更好啦,记着连接时,ip,端口号一定要输入正确啊。
[root@localhost ~]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
216c535c46c74010fb04ee9603f1dd6d5ab1f638e830bb6e862d068875d15a6a
[root@localhost ~]# docker exec -it mysql bash//
root@216c535c46c7:/# mysql uroot -p
Enter password:
ERROR 1049 (42000): Unknown database 'uroot'
root@216c535c46c7:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
连接成功后,请创建一个数据库,把你的数据库里的表啊什么的,导入到mysql容器的数据库,再刷新一下就ok了
3,我们再次进入到docker里的mysql容器里,查看我们导入的数据库traceability,说明导入数据库成功。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| traceability |
+--------------------+
5 rows in set (0.00 sec)
mysql> use traceability;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------------+
| Tables_in_traceability |
+------------------------+
| certificates |
| firm_info |
| foodmanagent |
| purchase |
| qualification |
| sample |
| t_user |
| test |
| user |
| worker |
+------------------------+
10 rows in set (0.00 sec)
4,打包成war文件之前,一定要把Java web项目里jabc.properties里面的连接mysql的配置更改一下,改成docker里的mysql容器的ip地址,(如database.url=jdbc:mysql://172.17.0.3:3306/traceability?useUnicode=true&characterEncoding=utf-8)如果用到了mybatis,也要把配置文件修改 一下,查看mysql容器的ip地址,可以用命令docker insepect mysql(容器名称)进行查看。
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/5c0037e88199",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "46c2952c3f43370b3478bc45fb0c2cb4cfef12342542fcbe44bb2269d7fc83e4",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "6e2ba28ae4226a6b07e6f18f3169e4f38822d06a0d40d33e7231b4f8f4d82375",
"EndpointID": "46c2952c3f43370b3478bc45fb0c2cb4cfef12342542fcbe44bb2269d7fc83e4",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",//mysql容器的ip
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03"
}
4、然后打包成war文件,部署到docker里tomcat容器里的webapps目录下,不会把Java web项目打包成war文件,可以参考我的上篇博客,https://blog.csdn.net/liqz666/article/details/82180581,然后在地址栏入http://192.168.130.122:/tracingfoood/userlogin.jsp 输入用户密码,登陆成功。访问到后台,说明你在docker里真正的完整地部署了一个Java web应用程序。
已完结。