Accessing app running in Docker container on macOS Big Sur host machine from other machine in local network

Hi experts,

I am running a Mosquitto broker for test purposes on a MacOS/Big Sur machine in a local network. The Docker system environment is:

  • Docker desktop version: 3.2.1 (61626)
  • The Mosquitto server I use is the standard Docker image from Docker Hub.

To start the container I used the command

docker run -it -p 1883:1883 eclipse-mosquitto mosquitto -c /mosquitto-no-auth.conf

Locally (on the Mac host, I can communicate with the server as expected. For example, executing the following Python script for the sender/producer:

import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("localhost", 1883, 60)
client.publish("topic/test", "Hello world!")
client.disconnect()

And the script for a listener/subscriber:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("topic/test")

def on_message(client, userdata, msg):
    if msg.payload.decode() == "Hello world!":
        print("Received HELLO WORLD!")
        client.disconnect()

client = mqtt.Client()
client.connect("localhost", 1883, 60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()`

works fine.

However, I want to connect to the server in the container from a Windows machine in the same local network. This fails if I replace the “localhost” above with the IP of my Mac-machine, the container is not reachable.

How can I reach the container/mosquitto server from the Windows PC?