docker安装mysql本地关联,如何将本地托管的MySQL数据库与Docker容器连接

Through docker-compose.yml I am able to run the application. Now we want to move the application to production, But we don't want to use the container database. So is there any way so that I can connect my local MySQL database with the application using docker-compose?

My docker-compose.yml looks like:

version: '3'

services:

web-app:

build:

context: .

dockerfile: web-app/Dockerfile

ports:

- 8080:8080

links:

- app-db

app-db:

build:

context: .

dockerfile: app-db/Dockerfile

environment:

- MYSQL_ROOT_PASSWORD=password

- MYSQL_DATABASE=Optimize

ports:

- 3306:3306

Instead of app-db part I just want to connect to my locally hosted mysql database.

解决方案

Find the host machine ip in the docker network. If you use docker-compose.yml version: "3" it's probably that that IP is: 172.18.0.1, but confirm it searching for the "Gateway" of your container (your host):

docker inspect | grep Gateway

"Gateway": "",

"IPv6Gateway": "",

"Gateway": "172.18.0.1",

"IPv6Gateway": "",

So inside your docker application point to MySQL as this: 172.18.0.1:3306 (maybe in a configuration file). Take into account that that IP is fixed as long as the docker network still the same (the network is created by docker-compose, and it is not removed unless you do docker-compose down)

Also, check that your MySQL is listening to all of its interfaces. In your my.cnf search for bind-address that should be 0.0.0.0 (consider security issues if your server has public IP).

As an alternative you can bring to the container the same networking as your host, in order to share the localhost, so the container will find mysql there. Use network mode as "host":

version: '3'

services:

web-app:

build:

context: .

dockerfile: web-app/Dockerfile

ports:

- 8080:8080

network_mode: "host"

Then, point in your hibernate.properties to mysql as this: localhost:3306

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值