在生产环境下难免会在容器内访问宿主服务,此时只需要使用host.docker.internal 即可,如ping host.docker.internal 输出如下
PING host.docker.internal (192.168.65.2): 56 data bytes
64 bytes from 192.168.65.2: seq=0 ttl=37 time=1.013 ms
64 bytes from 192.168.65.2: seq=1 ttl=37 time=3.942 ms
64 bytes from 192.168.65.2: seq=2 ttl=37 time=4.482 ms
64 bytes from 192.168.65.2: seq=3 ttl=37 time=4.490 ms
64 bytes from 192.168.65.2: seq=4 ttl=37 time=3.855 ms
64 bytes from 192.168.65.2: seq=5 ttl=37 time=3.798 ms
64 bytes from 192.168.65.2: seq=6 ttl=37 time=4.037 ms
64 bytes from 192.168.65.2: seq=7 ttl=37 time=4.214 ms
64 bytes from 192.168.65.2: seq=8 ttl=37 time=3.870 ms
64 bytes from 192.168.65.2: seq=9 ttl=37 time=3.823 ms
64 bytes from 192.168.65.2: seq=10 ttl=37 time=3.860 ms
可以看到192.168.65.2 即为宿主ip
注意 :如果你的docker是docker desktop此时输出的并不是你电脑的本机ip,因为docker desktop还是依赖虚拟机,因此输出的只是虚拟机的ip
|