This is from a note I wrote in 2017, there is likely a better way to do this in modern Docker.
Inside the container run
sudo ifconfig lo0 alias 10.0.2.2. This will add 10.0.2.2 as an alias for the loopback interface
Now, the container can talk to the host by targeting 10.0.2.2