0

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()
6
  • 2
    "I don’t receive any messages" - how are you confirming this. My guess is that this (more details in this) might be your issue. If not, please include your dockerfile and compose.yaml files so we can reproduce the issue. Commented Feb 20 at 21:12
  • 1
    Do you get any logging output from the container? What happens if you add on_error callbacks? Commented Feb 20 at 21:45
  • @Brits I added host network mode to the container in the .yaml file and I received the messages, but I lost the internal connection with my registry container. there is a problem with networks or my yaml conf, however, the strange thing is that only happens on my server. I will upload the files. Commented Feb 21 at 7:56
  • @hardillb there is no specific error shown, the only message I get is "Connected with result code 0" Commented Feb 21 at 15:36
  • 1
    It is not only an issue of your application running in docker. We also need to know where your broker is accessible. Do not expect 'localhost' to work. Commented Feb 21 at 21:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.