dockermosquittopahopahocpppython-paho

Paho C++ MQTT Client unable to connect to Mosquitto on Ubuntu Docker Image (Python Client Works)


I am facing this issue for a couple of days and I am unable to reproduce it outside the Ubuntu Docker container.
Please refer to this simplified diagram of my architecture:

Diagram

As you can see, I have everything running inside the Docker container.
This is not the typical issue of not being able to connect to inside the container, the whole application runs on the local network of the Docker container.

The Ubuntu Image is the official one with just some packages installed so that everything works (nothing too fancy, just python3 and some c++ tools).
It is running Mosquitto 2.0.15 with the following configuration:

listener 1883
protocol mqtt
allow_anonymous true
log_dest file /home/user/mosquitto.log

So, in summary, I am running an unsecured MQTT broker in the default port 1883. I also added a log file in an attempt to debug what is going on.

On my Python Client, the connection is made in the simplest way possible:

...
self.mqtt_client = mqtt.Client(client_id="Client Id")
self.mqtt_client.connect("localhost", 1883, 6000)
...
self.mqtt_client.loop_start()
...

This Client is able to connect to the broker with no issue at all. Subscribe and Publish works perfectly too!

On my C++ Client, I try to keep it simple as well:

...
mqtt::async_client MQTTClient("tcp://localhost:1883", "Another Client Id");
MQTTClient.connect()->wait();
...

Yet, this Client fails every attempt to connect to the broker:

terminate called after throwing an instance of 'mqtt::exception'
  what():  MQTT error [-1]: TCP/TLS connect failure
Aborted (core dumped)

Although it looks like a SSL/TLS error, this is actually the generic error for when the MQTT broker is not found (If I stop the broker on my local machine I get the same error).
Checking the MQTT broker logs (verbose ON), it does not record anything regarding the attempt to connect from C++ Client:

1672874968: mosquitto version 2.0.15 starting
1672874968: Config loaded from /etc/mosquitto/conf.d/default.conf.
1672874968: Opening ipv4 listen socket on port 1883.
1672874968: mosquitto version 2.0.15 running

(I do get log messages when connecting with Python Client)

On my own Linux machine I am unable to reproduce this issue. Both clients work fine.
I am using the same configuration for the Mosquitto broker and the Clients are exactly the same.

Already tried to play with the IP used to connect to the broker (localhost/127.0.0.1/0.0.0.0) and using different ports, the result is the same.

It is weird that the Python Client can connect with no issue at all.
At this point, I am fairly confident that the issue must be something Docker-specific and how it handles TCP sockets. But I am unable to find anything helpful online.

I can easily check that the port is open:

lsof -i -P -n | grep :1883
mosquitto  56 user 5u  IPv4 1633274841      0t0  TCP 127.0.0.1:1883 (LISTEN)

Any ideas what is going on? Or debug tools ideas? Thanks in advance!

I have tried:


Solution

  • I finally found out what my issue was.

    I had a HTTP proxy configured and Paho C will automatically use this proxy.
    It is detecting by checking if "http_proxy" environment variable is set.

    My solution was simply unset this variable:
    unset http_proxy

    Interestingly, the Python MQTT client does not have this behavior.
    And for that reason, it worked perfectly!