Docker for Mac
does not provide access to container IP from macOS host.
Reference Known limitations, use cases, and workarounds.
There is a complex solution,
which is also my source of inspiration. The main idea is to build a VPN between the macOS host and the docker virtual machine.
+------------+ +-----------------+
| | | Hypervisor |
| macOS | | +-----------+ |
| | | | Container | |
| | vpn | +-----------+ |
| VPN Client |<-------->| VPN Server |
+------------+ +-----------------+
But the macOS host cannot access the container, the vpn port must be exported and forwarded. Since the VPN connection is duplex, so we can reverse it.
+------------+ +-----------------+
| | | Hypervisor |
| macOS | | +-----------+ |
| | | | Container | |
| | vpn | +-----------+ |
| VPN Server |<-------->| VPN Client |
+------------+ +-----------------+
Even so, we need to do more extra work to use openvpn, such as certificates, configuration, etc. All I want is to access the container via IP, why is it so cumbersome. No need for security, multi-clients, or certificates, just connect.
+------------+ +-----------------+
| | | Hypervisor |
| macOS | | +-----------+ |
| | | | Container | |
| | udp | +-----------+ |
| TUN Server |<-------->| TUN Client |
+------------+ +-----------------+
Install mac client of mac-docker-connector.
$ brew tap wenjunxiao/brew
$ brew install docker-connector
Config route of docker network
$ 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
Start the service
$ sudo brew services start docker-connector
Install docker front of mac-docker-connector
$ docker pull origin wenjunxiao/mac-docker-connector
Start the docker front. The network must be host
, and add NET_ADMIN
capability.
$ docker run -it -d --restart always --net host --cap-add NET_ADMIN --name connector mac-docker-connector
If you want to expose the containers of docker to other pepole, Please reference docker-accessor