Skip to content

Instantly share code, notes, and snippets.

@makuk66
Last active March 6, 2024 12:24
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save makuk66/8380c901a9a620df7023 to your computer and use it in GitHub Desktop.
Save makuk66/8380c901a9a620df7023 to your computer and use it in GitHub Desktop.

This is a demo of how to talk between programs running on a Mac, and programs running under Docker on a VM on the same Mac as installed by Docker Toolbox.

Just add a route to your containers using the VM as the gateway. Demo:

  • on the mac, run Docker Quickstart Terminal from the latest Docker Toolbox. This launches the virtualbox VM, and configures your shell
  • in that shell, figure out the IP address of the VM:
mak@crab 493 ~ $ docker-machine inspect default -f '{{ .Driver.IPAddress }} '
192.168.99.100 
  • figure out the addresses used by the docker0 bridge in the VM:
mak@crab 495 ~ $ docker-machine ssh default ip -4 addr list docker0
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
  • now add a route for that subnet on your mac:
mak@crab 496 ~ $ sudo route add 172.17.0.0/16  192.168.99.100
Password:
add net 172.17.0.0: gateway 192.168.99.100
  • run up a container, print its IP address, and run a server listening on port 6655:
root@56432ab778ae:/# ip -4 addr list eth0
10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
root@56432ab778ae:/# nc.openbsd -l -p 7768
  • from the mac whisper sweet nothings into its ear:
mak@crab 548 ~ $ echo "shi" | nc 172.17.0.2 7768
mak@crab 549 ~ $ 

and watch it come out the other end:

root@56432ab778ae:/# nc.openbsd -l -p 7768
shi

And it works the other direction too:

mak@crab 488 ~ $ ifconfig 
...
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
...
mak@crab 491 ~ $ nc -l 0.0.0.0 7766
 root@56432ab778ae:/# nc 192.168.88.9 7766
jo
mak@crab 491 ~ $ nc -l 0.0.0.0 7766
jo
@seasonsolt
Copy link

seasonsolt commented Mar 27, 2020

In my condition,after 'route add' operation,vm's iptables must by be set by:
sudo iptables -I DOCKER-USER -j ACCEPT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment