admin管理员组文章数量:1289828
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()
本文标签: Unable to Receive MQTT Messages Inside Docker Container on Ubuntu ServerStack Overflow
版权声明:本文标题:Unable to Receive MQTT Messages Inside Docker Container on Ubuntu Server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741412097a2377296.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论