简体   繁体   English

为什么 host.docker.internal 没有解析我的内部 IP

[英]Why host.docker.internal is NOT resolving my internal IP

I'm trying to connect to host OS MySQL via host.docker.internal, I'm able to connect if i directly mention my internal IP in Laravel application hosted inside docker container.我正在尝试通过 host.docker.internal 连接到host OS MySQL ,如果我在 docker 容器内托管的 Laravel 应用程序中直接提及我的内部 IP,我就可以连接。

OS / ENVIRONMENT: Host operating system and version: MacOS Monterey 12.5.1 Docker desktop version: 4.12.0 (85629) Docker desktop engine: Engine: 20.10.17 Docker desktop compose version: v2.10.2操作系统/环境:主机操作系统和版本:MacOS Monterey 12.5.1 Docker 桌面版本:4.12.0 (85629) Docker 桌面引擎:引擎:20.10.17 Docker 桌面组合版本:v2.10.2

Problem: These are the steps i took to connect my Laravel application inside docker to my host OS MySQL. I successfully managed to connect my application via internal IP address of my Host OS, but the internal IP keep changing and its kind of getting dificult to keep changing the DB_HOST inside laravel.env each time the IP change.问题:这些是我将 docker 中的 Laravel 应用程序连接到我的主机操作系统 MySQL 所采取的步骤。我成功地通过主机操作系统的内部 IP 地址连接了我的应用程序,但是内部 IP 不断变化并且它变得越来越困难每次 IP 更改时,请继续更改 laravel.env 中的 DB_HOST。 so i want to use host.docker.internal but i won't work.所以我想使用host.docker.internal但我不会工作。

Steps:脚步:

1: docker-compose down (Delete all the containers) 1: docker-compose down (删除所有容器)

2: I removed the devilbox.env port HOST_PORT_MYSQL= 2:我去掉了devilbox.env port HOST_PORT_MYSQL=

3: I changed the port of my host OS MySQL to 3306 and using sequel ace i successfully connected to mysql with these credentials 3:我将host OS MySQL to 3306并使用 sequel ace 我使用这些凭据成功连接到 mysql

Host: 127.0.0.1 user: root database: hanger port: 3306 Host: 127.0.0.1 user: root database: hanger port: 3306

4: In order to connect from docker to my Host OS MySQL i had to edit my my.cnf file OR in this case created a new one for MySQL here the my.cnf 4:为了从 docker 连接到我的主机操作系统 MySQL,我必须编辑我的my.cnf文件,或者在这种情况下为 MySQL 创建一个新的 my.cnf

[mysqld] bind_address = 0.0.0.0 # default is 127.0.0.1 Change to 0.0.0.0 to allow remote connections [mysqld] bind_address = 0.0.0.0 # default is 127.0.0.1 Change to 0.0.0.0 to allow remote connections

5: Restarted the MySQL server and confirmed that MySQL can now listen to all IP's and NOT just localhost 5:重新启动 MySQL 服务器并确认 MySQL 现在可以侦听所有 IP 而不仅仅是本地主机

6: used this command 6:使用这个命令

netstat -anp tcp | grep 3306 OR.netstat -ap tcp | grep -i "listen"

tcp4 0 0 127.0.0.1.3306 127.0.0.1.52469 ESTABLISHED tcp4 0 0 127.0.0.1.52469 127.0.0.1.3306 ESTABLISHED tcp4 0 0 127.0.0.1.3306 127.0.0.1.52468 ESTABLISHED tcp4 0 0 127.0.0.1.52468 127.0.0.1.3306 ESTABLISHED tcp4 0 0 127.0.0.1.3306 127.0.0.1.52464 ESTABLISHED tcp4 0 0 127.0.0.1.52464 127.0.0.1.3306 ESTABLISHED tcp4 0 0 *.3306. LISTEN tcp4 0 0 127.0.0.1.3306 127.0.0.1.52469 ESTABLISHED已建立tcp4 0 0 127.0.0.1.52469 127.0.0.1.3306 ESTABLISHED已建立tcp4 0 0 127.0.0.1.3306 127.0.0.1.52468 ESTABLISHED tcp4 0 0 127.0.0.1.52468 127.0.0.1.3306 ESTABLISHED tcp4 0 0 127.0.0.1.3306 127.0.0.1.52464 ESTABLISHED tcp4 0 0 127.0.0.1.52464 127.0.0.1.3306 ESTABLISHED tcp4 0 0 *.3306. LISTEN tcp4 0 0 *.3306. LISTEN tcp46 0 0 *.33060. LISTENtcp4 0 0 *.3306. LISTEN tcp46 0 0 *.33060. LISTEN tcp46 0 0 *.33060. LISTEN tcp4 0 0 192.168.18.190.3306 192.168.18.190.52566 TIME_WAIT tcp4 0 0 192.168.18.190.3306 192.168.18.190.52567 TIME_WAIT tcp4 0 0 192.168.18.190.3306 192.168.18.190.52568 TIME_WAIT tcp46 0 0 *.33060. LISTEN tcp4 0 0 192.168.18.190.3306 192.168.18.190.52566 TIME_WAIT tcp4 0 0 192.168.18.190.3306 192.168.18.190.52567 TIME_WAIT tcp4 0 0 192.168.18.190.3306 192.168.18.190.52568 TIME_WAIT

7: Once its confirmed that 3306 is listeing need to create a MySQL user which would be connected from other than localhost 7:一旦确认3306正在监听,需要创建一个 MySQL 用户,该用户将从本地主机以外的其他地方连接

8: In mysql shell i executed these queries, since I'm using MySQL 8.0.27 the creating user and granting previliges must be in seperate queries. 8:在 mysql shell 中,我执行了这些查询,因为我使用的是 MySQL 8.0.27,创建用户和授予权限必须在单独的查询中进行。

CREATE USER 'root'@'%' IDENTIFIED BY 'root'; // remember this root password we will use it in Laravel.env CREATE USER 'root'@'%' IDENTIFIED BY 'root'; // remember this root password we will use it in Laravel.env GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; CREATE USER 'root'@'%' IDENTIFIED BY 'root'; // remember this root password we will use it in Laravel.env GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;

9: To make sure the root@% user is created type 9: 确保创建了root@%用户类型

SELECT User, Host FROM mysql.user; there are two root users one with host set to localhost and second one is %two root users one with host set to localhostsecond one is %

10: Now its time to Edit Laravel.env MySQL section 10:现在是编辑Laravel.env MySQL 部分的时候了

DB_CONNECTION=mysql DB_HOST=192.168.18.190 // my host machine internal ip (host.docker.internal not working) DB_PORT=3306 DB_DATABASE=hanger DB_USERNAME=root DB_PASSWORD=root DB_CONNECTION=mysql DB_HOST=192.168.18.190 // 我的主机内部 ip (host.docker.internal 不工作) DB_PORT=3306 DB_DATABASE=hanger DB_USERNAME=root DB_PASSWORD=root

Note: my DB_HOST did not work with 127.0.0.1 OR host.docker.internal so i thought it may work with my local IP, which it did.注意:我的DB_HOST不适用于127.0.0.1 OR host.docker.internal ,所以我认为它可能适用于我的本地 IP,它确实如此。

11: To find out my local IP on MAC go to system preferences >.network > My wifi connection > advanced > TCP/IP > under IPv4 192.168.43.182 11:要在MAC go上找到我本地的IP到系统偏好设置>.network>我的wifi连接>高级>TCP/IP>下IPv4 192.168.43.182

The thing I'm concerned about is that my local IP keep changing, and as per the documentation The following sections will give you the IP address and/or the CNAME where the host os can be reached from within a container.我担心的是我的本地 IP 不断变化,并且根据文档The following sections will give you the IP address and/or the CNAME where the host os can be reached from within a container. https://devilbox.readthedocs.io/en/latest/advanced/connect-to-host-os.html#docker-18-03-0-ce-and-docker-compose-1-20-1 The docker should be able to connect through host.docker.internal to my Host machine, which it does not and i don't know why. https://devilbox.readthedocs.io/en/latest/advanced/connect-to-host-os.html#docker-18-03-0-ce-and-docker-compose-1-20-1 docker 应该能够通过 host.docker.internal 连接到我的主机,但我不知道为什么。 Can you please anyone please point me in the direction what should i do to figure out this issue?你能请任何人指点我应该怎么做才能解决这个问题吗?

Don't know the exact reasoning why does it work on some mac machines and doesn't on some, but you can force docker to map host.docker.internal by adding "host.docker.internal:host-gateway" under extra_hosts in your docker-compose. You should be able to use it post this.不知道为什么它在某些 mac 机器上工作而在某些机器上不起作用的确切原因,但是您可以通过在 extra_hosts 中添加"host.docker.internal:host-gateway"来强制 docker 到 map host.docker.internal您的 docker-compose。您应该可以在发布后使用它。

Same problem here, the "host.docker.internal" is for development purpose and does not work in a production environment outside of Docker Desktop.同样的问题,“host.docker.internal”用于开发目的,在 Docker 桌面之外的生产环境中不起作用。

https://docs.docker.com/desktop.networking/ https://docs.docker.com/desktop.networking/

1]: https://i.stack.imgur.com/4MnYp.jpg 1]: https://i.stack.imgur.com/4MnYp.jpg

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

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