如何通过 Docker 连接到本地 MySQL 服务器? [英] How to connect to local MySQL server through Docker?

查看:62
本文介绍了如何通过 Docker 连接到本地 MySQL 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于如何通过 Docker 连接到本地服务的更普遍的问题.在 Github 问题 here 中有一个类似的问题,似乎没有任何解决方案.我真正想要的是能够针对我的本地开发 MySQL 服务器在本地进行开发,然后一旦我准备好部署,就可以针对新创建的部署候选 docker 映像在本地进行测试.

This is more a general question for how to connect to local services through Docker. There's a similar question in a Github issue here that doesn't seem to have any resolution. What I'm really looking for is to be able to do development locally against my local development MySQL server, then once I'm ready to deploy, to test locally against a newly created deploy candidate docker image.

理想情况下,两者都从同一个地方获取设置,所以我可以放置 mysql_server: host_ip.这似乎是一个典型的用例.目前有这种可能吗?

Ideally, both get settings from the same place as well, so I could put mysql_server: host_ip. This seems like a typical use case. Is anything like this currently possible?

我专门将 Boot2Docker 与在我的主机 mac 的 OS X Yosemite 上运行的 MySQL 服务器一起使用,而不是在容器中.不过,如果能为未来的读者提供更通用的答案会很酷.

I'm using Boot2Docker specifically with MySQL server running on my host mac's OS X Yosemite NOT in a container. Would be cool to have a more general answer for future readers though.

推荐答案

Docker CLI 文档 给出这个解决方案(假设您在 Linux 主机上运行):

The Docker CLI docs give this solution (which assumes you are running on a Linux host with ):

有时您需要从容器内连接到 Docker 主机.要启用此功能,请使用 --add-host 标志将 Docker 主机的 IP 地址传递给容器.要查找主机地址,请使用 ip addr show 命令.

Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the --add-host flag. To find the host’s address, use the ip addr show command.

传递给 ip addr show 的标志取决于您在容器中使用的是 IPv4 还是 IPv6 网络.使用以下标志为名为 eth0 的网络设备检索 IPv4 地址:

The flags you pass to ip addr show depend on whether you are using IPv4 or IPv6 networking in your containers. Use the following flags for IPv4 address retrieval for a network device named eth0:

$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print $2}' | cut -d / -f 1`
$ docker run  --add-host=docker:${HOSTIP} --rm -it debian

然后容器内的名称 docker 将映射到主机的 IP 地址.对于您的情况,您可以使用 docker run --add-host=mysql_server:$(hostip) ...

Then the name docker inside the container will map to the host's IP address. For your case, you could use docker run --add-host=mysql_server:$(hostip) ...

如果使用 Boot2Docker,它会在预定义的地址设置到主机的映射,因此在该平台上,与上述等效的只是一个命令:

If using Boot2Docker, it sets up a mapping to the host at a predefined address, so on that platform the equivalent to the above is just the one command:

$ docker run  --add-host=docker:192.168.59.3 --rm -it debian

这篇关于如何通过 Docker 连接到本地 MySQL 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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