How to access services running on your machine from your broker running in Docker

Do you need to access your local machine’s network from inside your broker running on Docker?

For instance, I wanted to create a REST endpoint so I could send some data from an RDP in a broker running in Docker. The problem is that you can’t use localhost or 127.0.0.1

For example, if I start my REST endpoint at port 9010 on my local machine, I can do
telnet localhost 9010
And everything works fine.

However, from a Docker container (in this case an Ubunutu image), life is not so good:
container# telnet localhost 9010
Trying 127.0.0.1...
Trying ::1...
telnet: Unable to connect to remote host: Cannot assign requested address

That’s because localhost is local to the container.

So I need to identifiy the local machine’s network to the container. It’s pretty simple, instead of using localhost short cuts, just specify your machine’s IP address:

host# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.28.138.196 netmask 255.255.240.0 broadcast 172.28.143.255
...

container# telnet 172.28.138.196 9010
Trying 172.28.138.196...
Connected to 172.28.138.196.

So now I can just set my RDP Rest Consumer to send to 172.28.138.196.

Nice tips ?

But I use this more on my Mac host.docker.internal ?

I’ve found host.docker.internal to be unreliable - it often gives an address that appears to have nothing to do with any network I know! But I’m using WSL2 on Windows, which might be why…