无法从Docker容器连接到数据库 [英] Can't connect to db from docker container

查看:76
本文介绍了无法从Docker容器连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器和dockerized项目上安装了MySQL服务器,该数据库不可公开访问,只能从服务器内部访问.本地非dockerized应用也使用它.我想从docker内部连接到它,但仍然无法公开访问,我尝试了172.17.0.1,但连接被拒绝.当前的bind_address为127.0.0.1,您建议bind_address为??

I have MySQL server installed on a server and dockerized project, The DB is not publicly accessible, only from inside the server. It's used by local non dockerized apps too. I want to connect to it from inside the docker with it remaining not publicly accessible, I tried 172.17.0.1 but I get connection refused. the current bind_address is 127.0.0.1, What do you suggest the bind_address would be ??

推荐答案

您可以通过 host 网络模式运行该应用程序,这将使容器能够连接到主服务器的本地主机(docker主机),同时保持 bind-address 指向 127.0.0.1 .

You can run the application through the host network mode which will make the container able to connect to the localhost of your main server (the docker host) while keeping the bind-address points to 127.0.0.1.

因此,如果您使用的是docker cli,则需要像这样运行您的应用程序:

So you need to run your application like this if you are using docker cli:

docker run --network=host myappimage

如果使用 docker-compose ,则将在您的服务中使用

In case of docker-compose you will use is in your service:

network_mode: host

这篇关于无法从Docker容器连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆