Dockerized Web应用程序连接到主机上的MySQL DB [英] Dockerized web app connecting to MySQL DB on host
问题描述
我想将我的应用程序部署迁移到docker / docker hub。目前的生产设置是在运行Apache,Tomcat和MySQL的两个Linux服务器上负载平衡流量。 Java Web应用程序通过JDBC连接到主服务器和从属MySQL数据库。
为了最小化这一变化的影响,作为第一步,我正在集中化tomcat和Web应用程序。离开Apache和MySQL就像现在一样在主机上运行。
我遇到的问题是将Docker容器中运行的应用程序与MySQL在主机上运行的数据库。
目前我可以做的最好的就是连接到docker容器,获取它的IP地址,然后使用SQL GRANT来允许该IP地址访问数据库。这样做的麻烦是,每次运行容器时,您都可能获得一个新的IP地址,因此每次都需要运行一个授权。
我一直在玩这个东西有一段时间,我不敢相信没有比上面提到的更好的解决方案。
任何人都做了这样的事情?任何人都有任何关于如何最好地解决这个问题的建议。
修改
有人建议从Docker内容器,我如何连接到本机的机器?是这个的重复 - 但基础的问题仍然存在。每次从映像创建一个Docker容器时,我可能会获得一个需要被授予访问数据库的容器的新IP地址。我想尝试避免这样做。
非常感谢
您应该使用dockerized MySQL服务器和Docker的链接
命令连接两个容器。
首先运行MySQL :
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD = my-secret-pw -d mysql
/ p>
然后运行您的应用程序与链接
:
docker run --name some-app --link some-mysql:mysql -d app-that-uses-mysql
现在您可以从环境变量获取数据库的地址和端口:
docker exec -it some-app printenv
寻找类似的东西: MYSQL_PORT_3306_TCP_PORT
。现在您只需要在应用程序中使用这些变量。
有关 Docker MySQL Repo 和 Docker文档。
I want to migrate my application deployment to use docker/docker hub. Current production set up is to load balance traffic across 2 Linux servers, running Apache, Tomcat and MySQL. The Java web application connects to a master+slave MySQL DB via JDBC.
To minimize the impact of this change, as a first step I am looking at containerizing the tomcat and web application. Leaving Apache and MySQL running on the host(s) as it does now.
The problem I am encountering is connecting the application, running in the docker container, with the MySQL DB running on the host.
The best I can do at the moment is connect to the docker container, get it's IP address and then use an SQL GRANT to allow that IP address access to the DB. The trouble with this is that every time you run a container you potentially get a new IP address and therefor have to run a grant each time.
I've been playing around with this stuff for a while and I can't believe there is not a better solution than what I have proposed above.
Anyone done anything like this? Anyone got any suggestions as to how to best approach this problem.
Edit It's been suggested that "From inside of a Docker container, how do I connect to the localhost of the machine?" is a duplicate of this - but the underlying problem still exists. Every time I create a Docker container from an image I potentially get an new IP address for the container that needs to be granted access to the DB. I wanted to try and avoid having to do this.
Many thanks
You should use dockerized MySQL server and Docker's link
command which connects two containers.
First run your MySQL:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
Then run your app with link
:
docker run --name some-app --link some-mysql:mysql -d app-that-uses-mysql
Now you can get database's address and port from environment variables:
docker exec -it some-app printenv
Look for something like: MYSQL_PORT_3306_TCP_PORT
. Now you just need to use these variables in your app.
More information on Docker MySQL Repo and Docker docs.
这篇关于Dockerized Web应用程序连接到主机上的MySQL DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!