It can be said that it is very convenient for docker for mac to run the local development environment.
But Docker for Mac has always had a problem since it was born, that is, docker0 cannot be seen on the host machine, and the network where the container is located cannot be accessed, which means that the IP address assigned by Docker to the Container cannot be pinged. Regarding this issue, the official document has a description:Known limitations, use cases, and workarounds
For the Container started by docker run, the corresponding service port is usually mapped through the -p parameter, and it is generally not encountered in the case of directly accessing the container IP.
But when we run multiple microservices in docker and want to debug locally, it can’t be achieved on Mac.
Solution:
Use docker-connector
First install docker-connector on the Mac side through brew
brew install wenjunxiao/brew/docker-connector
Then execute the following command to add all bridge networks of docker to the route
docker network ls --filter driver=bridge --format "{{.ID}}" | xargs docker network inspect --format "route {{range .IPAM.Config}}{{.Subnet}}{{end}}" >> /usr/local/etc/docker-connector.conf
You can also manually modify the route in the /usr/local/etc/docker-connector.conf file, the format is
route 172.17.0.0/16
The routing subnet determines which containers you can access
After the configuration is complete, start the service directly (sudo is required, the routing configuration can still be modified after it is started, and it will take effect immediately without restarting the service)
sudo brew services start docker-connector
Then use the following command to run wenjunxiao/mac-docker-connector on the docker side, you need to use the host network and allow NET_ADMIN
docker run -it -d --restart always --net host --cap-add NET_ADMIN --name connector wenjunxiao/mac-docker-connector
Choose a container IP to test, my test IP is 172.100.0.10, and start an HTTP service in the corresponding container
$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
Ping and access http services directly on the host machine
$ ping 172.100.0.10
PING 172.100.0.10 (172.100.0.10): 56 data bytes
64 bytes from 172.100.0.10: icmp_seq=0 ttl=63 time=0.837 ms
64 bytes from 172.100.0.10: icmp_seq=1 ttl=63 time=1.689 ms
64 bytes from 172.100.0.10: icmp_seq=2 ttl=63 time=2.793 ms
64 bytes from 172.100.0.10: icmp_seq=3 ttl=63 time=2.333 ms
Re-verify HTTP service
$ curl -si -w "%{http_code}" http://172.100.0.10:8080 -o /dev/null200