Gray
Gray

Reputation: 586

Unable to Receive MQTT Messages Inside Docker Container on Ubuntu Server

I have a code that works fine on my Ubuntu server, but when I run it inside a container, I don’t receive any messages. It seems like the container doesn’t have access to the MQTT broker.

However, I checked, and there is a connection from inside the container to the external broker. I’m confused as to why I’m not getting messages.

The only way I got it to work was by using network_mode: host in the yaml file, but I can't use this in production. Do you have any idea what might be causing this or how to fix it?

connection check:

nc -zv mqtt.eclipseprojects.io 1883
Connection to mqtt.eclipseprojects.io 1883 port [tcp/*] succeeded!

the code:

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Callback function when the client connects to the broker
def on_connect(client, userdata, flags, rc):
    logging.info(f"Connected with result code {rc}")
    client.subscribe("mytopic/123")  # Subscribe to the specified topic

# Callback function when a message is received
def on_message(client, userdata, msg):
    logging.info(f"Received message: {msg.payload.decode()} from topic: {msg.topic}")

def main():
    # Load MQTT configuration
    config = {
        "dh_login": "login",
        "dh_password": "pass",
        "host": "mqtt.eclipseprojects.io",
        "port": 1883,
        "topic": "topic/123",
        "qos": 1,
        "mqtt_username": "user",
        "mqtt_password": "pass" 
    }

    # MQTT Broker configuration
    host = config["host"]
    port = config["port"]
    topic = config["topic"]
    qos = config["qos"]
    
    # Create an MQTT client
    client = mqtt.Client()

    # Set MQTT username and password
    client.username_pw_set(config["mqtt_username"], config["mqtt_password"])

    # Set the callback functions
    client.on_connect = on_connect
    client.on_message = on_message

    # Connect to the MQTT broker
    client.connect(host, port, 60)
    
    # Start the loop to receive messages
    client.loop_forever()

if __name__ == "__main__":
    main()

Upvotes: 0

Views: 44

Answers (0)

Related Questions