The vice versa can easily be done by including -p flag. But the way to expose ports of host machine to a docker container is bit convoluted, but can be done.
The steps are:
- Create an alias for the ip you want to use.
sudo ifconfig lo0 alias 172.16.123.1
- add the flag to docker:
docker run <image> --add-host=docker.local:172.16.123.1
Or
add it in docker-compose as:
services:
service1:
extra_hosts:
- "docker.local:172.16.123.1"
- Use docker.local as host in the container.