简体   繁体   English

从macOS上的docker容器内连接到localhost mysql服务器

[英]Connecting to localhost mysql server from inside docker container on macOS

How can I connect to localhost mysql server from a docker container on macOS ? 如何从macOS上的docker容器连接到localhost mysql服务器? One way to do it using --add-host but that requires me to pass some name like "myhost". 使用--add-host执行此操作的一种方法,但需要我传递一些名称,如“myhost”。 Is there any way in macOS so that references to localhost from inside docker container actually refer to docker host ? 在macOS中是否有任何方法可以从docker容器内部引用localhost实际引用docker host?

在MacOS上, docker.for.mac.localhost提供特殊的DNS名称docker.for.mac.localhost ,它将解析为主机使用的内部IP地址。

You should be able to connect to MySql running on host machine using host machine actual IP address. 您应该能够使用主机实际IP地址连接到在主机上运行的MySql。 In MacOS, try to find your ip by command ifconfig . 在MacOS中,尝试通过命令ifconfig找到您的ip。 Mostly using IP assigned to en0 ie your ethernet interface should work. 主要使用分配给en0 IP,即您的以太网接口应该可以工作。 Just call that IP from within your container. 只需从容器中调用该IP即可。

*localhost or 127.0.0.1 or 0.0.0.0 doesnt call host machine as they are local to container itself. * localhost或127.0.0.1或0.0.0.0不会调用主机,因为它们是容器本身的本地机器。

use host.docker.internal 使用host.docker.internal

to connect to the host running the Docker. 连接到运行Docker的主机。 this works From docker version 18.03 onwards only and This is for development purposes and will not work in a production environment outside of Docker Desktop for Mac. 这适用于仅从docker版本18.03开始,这用于开发目的,不适用于Docker Desktop for Mac之外的生产环境。 ( refer the page https://docs.docker.com/docker-for-mac/networking/ for more info ) (有关详细信息,请参阅https://docs.docker.com/docker-for-mac/networking/页面)

sample connection string for oracle, jdbc:oracle:thin:@host.docker.internal:1521/orcl oracle的示例连接字符串,jdbc:oracle:thin:@ host.docker.internal:1521 / orcl

From inside of a Docker container, how do I connect to the localhost of the machine? 从Docker容器内部,如何连接到本机的本地主机?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM