看到一篇文章,mark一下。

On Docker for Mac the docker0 bridge does not exist, so other answers here may not work. All outgoing traffic however, is routed through your parent host, so as long as you try to connect to an IP it recognizes as itself (and the docker container doesn't think is itself) you should be able to connect. For example if you run this from the parent machine run:

ipconfig getifaddr en0

This should show you the IP of your Mac on its current network and your docker container should be able to connect to this address as well. This is of course a pain if this IP address ever changes, but you can add a custom loopback IP to your Mac that the container doesn't think is itself by doing something like this on the parent machine:

sudo ifconfig lo0 alias 192.168.46.49

You can then test the connection from within the docker container with telnet. In my case I wanted to connect to a remote xdebug server:

telnet 192.168.46.49 9000

Now when traffic comes into your Mac addressed for 192.168.46.49 (and all the traffic leaving your container does go through your Mac) your Mac will assume that IP is itself. When you are finish using this IP, you can remove the loopback alias like this:

sudo ifconfig lo0 -alias 192.168.46.49

One thing to be careful about is that the docker container won't send traffic to the parent host if it thinks the traffic's destination is itself. So check the loopback interface inside the container if you have trouble:

sudo ip addr show lo

In my case, this showed inet 127.0.0.1/8 which means I couldn't use any IPs in the 127.* range. That's why I used 192.168.* in the example above. Make sure the IP you use doesn't conflict with something on your own network.

shareimprove this answer

edited Jun 21 at 17:13

answered Aug 18 '16 at 19:30

Code Commander

9,39634248

  • will it cleanup these loop back settings after a system restart? – Robbo_UK Mar 5 '17 at 17:02

  • @Robbo_UK Yes, the loopback alias will be gone after you restart your mac. – Code Commander Aug 24 '17 at 22:19

  • host.docker.internal works like magic. Thanks mate! – ksealey Jul 2 at 13:26

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐