本文介绍了将在docker容器中运行的tomcat webapp连接到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有mysos服务器在centos主机中运行,我想在同一台主机的docker容器内的tomcat内部部署我的战争。任何想法如何从容器内部连接mysql。解决方案
这是你可以做什么来连接到数据库(mysql )从App(tomcat)。
有两种方式可以做到这一点
- 使用链接
- 手动方式通过向tomcat应用程序提供db的IP地址
示例-1(链接)
- 获取您的mysql容器:
- 您现在需要构建并运行必须
的应用程序容器,知道db实例在哪里。您将必须弄清楚您在
中提供应用程序中数据库的链接。它可以在代码
或属性文件中。您可以使用卷将该文件挂载到
应用程序容器中。您还可以在
飞行中修改它们,因为您可以在主机系统上访问它们。这个
是这样做的一个例子:docker
- 这里我们使用的图像应用程序,我正在从我的docker基本文件夹装载webapp文件夹
。这个文件夹有我的代码,我是
推送到tomcat的webapp文件夹。我还将
属性文件推送到容器上的适当位置,其中
mysql连接被提到如下:
jdbc.url = jdbc:mysql:// db:3306 / dbname。我们将容器的
的端口8080映射到主机的端口8080。我们正在使用容器应用程序映射容器
db。这将在/ etc / hosts文件中创建一个条目 - db
ipname。因此,即使
我们重新启动数据库容器(更改IP),两个容器也可以相互通信。链接将使用新的IP更新
主机条目。
示例2:
- 向上添加数据库容器
- 使用docker inspect -f{{.NetworkSettings.IPAddress}]获取数据库容器的IP }'container-name / ID
- 向上添加可以在主机上编辑jdbc url的卷的应用程序容器。
- 更改IP到db容器的IP,这应该能够建立连接
如果你想使用主机的mysql
如果您需要任何进一步的解释,请告诉我。
您可以浏览有关卷的主题,从Docker文档的链接:
I have mysql server running in centos host and I want to deploy my war in tomcat inside docker container in same host. Any idea how can I connect mysql from inside the container.
解决方案
Here is what you can do to connect to the DB (mysql) from the App (tomcat).There are two ways you can do this
- By using links
- Manual way by providing the IP address of db to the tomcat app
Example-1 (Links)
- Get your mysql container up:
- You now need to build and run the application container which mustknow where is the db instance. You will have to figure out where youprovide the link to the db in your application. It can be in the codeor in a property file. You can use volume to mount this file into theapplication container. You will also be able to modify them on thefly because they will be accessible to you on the host system. Thisis one example of doing this: docker
- Here we are using the image app where i am mounting webapp folderfrom my docker base folder. This folder is having my code which i ampushing to the tomcat's webapp folder. I am also pushing theproperties file to the appropiate location on the container where themysql connection is mentioned like this :"jdbc.url=jdbc:mysql://db:3306/dbname" . We are mapping port 8080 ofthe container to the port 8080 of the host. We are mapping containerdb with container app. This creates an entry on /etc/hosts file - "dbipname". So both containers can communicate with each other even ifwe restart the db container (which changes the IP). Links will updatethe host entry with the new IP.
Example 2:
- Up the db container
- Get the IP of the db container using this "docker inspect -f '{{ .NetworkSettings.IPAddress }}' container-name/ID"
- Up the app containers with volumes where you can edit the jdbc url on your host.
- Change the IP to db container's IP and this should be able to establish the connection
If you want to use Host's mysql
Do let me know if you need any further explanation.You can go through the topics on volumes, links from docker documentation: https://docs.docker.com/
这篇关于将在docker容器中运行的tomcat webapp连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!