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

查看:530
本文介绍了无法从 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 网络模式运行应用程序,这将使容器能够连接到您的主服务器的 localhost (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天全站免登陆