Partial successful connection to Mosquitto

I have been using mosquitto /mqttx/mqtt-explorer for years without issue on several RaspberryPi 5’s (RPi).

A device failed and the backups, so I am creating a fresh install using docker and Latest RPi OS (Bookworm). All applications are installed using compose files.

Mosquitto runs and the logs show no errors:

1763311480: mosquitto version 2.0.22 starting

1763311480: Config loaded from /mosquitto/config/mosquitto.conf.

1763311480: Starting in local only mode. Connections will only be possible from clients running on this machine.

1763311480: Create a configuration file which defines a listener to allow remote access.

1763311480: For more details see

1763311480: Opening ipv4 listen socket on port 1883.

1763311480: Opening ipv6 listen socket on port 1883.

1763311480: mosquitto version 2.0.22 running

The compose file is:

#mosquitto: compose.yaml
services:
mosquitto:
container_name: mosquitto
image: eclipse-mosquitto:latest
hostname: mosquitto
restart: unless-stopped
ports:
- “1883:1883”
- “9001:9001”
environment:
- TZ=Europe/London
networks:
- pi_net
volumes:
#      /path/on/host:/path/in/container
- ../../volumes/mosquitto/config/mosquitto.conf:/mosquitto/mosquitto.conf
- ../../volumes/mosquitto/data:/mosquitto/data
- ../../volumes/mosquitto/log:/mosquitto/log

networks:
pi_net:
external: true

… and the config file is:

# mosquitto.conf: Basic listener configuration

listener 1883
protocol mqtt
allow_anonymous true

#WebSocket listener

listener 9001
protocol websockets
allow_anonymous true

#Persistence

persistence true
persistence_location /mosquitto/data/

#Logging

log_dest file /mosquitto/log/mosquitto.log
log_type error
log_type warning
log_type notice
log_type information

I can check mqtt messages are sent/received using two pub/sub terminal windows & exec into the container:

docker exec -it mosquitto sh
terminal 1: mosquitto_sub -t “test”
terminal 2: mosquitto_pub -t “test” -m “Hello World…”

terminal 1 successful receives the test message. Conclusion: everything OK

Next test:

docker ps: (shows published ports:)

5d25fb638d15 eclipse-mosquitto:latest “/docker-entrypoint.…” About a minute ago Up About a minute 0.0.0.0:1883->1883/tcp, [::]:1883->1883/tcp, 0.0.0.0:9001->9001/tcp, [::]:9001->9001/tcp mosquitto

Yup, 1833 and 9001 are published. Conclusion: everything OK.

Next test: network ports:

sudo netstat -tlnp (abbreviated)

tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN 4969/docker-proxy
tcp 0 0 0.0.0.0:9001 0.0.0.0:* LISTEN 4983/docker-proxy
tcp6 0 0 :::1883 :::* LISTEN 4976/docker-proxy
tcp6 0 0 :::9001 :::* LISTEN 4990/docker-proxy

All looks good to me on both ipv4 and ipv6.

Conclusion: mosquitto IS working fine.

But when I come to use:

  • mqttx
  • mqtt-explorer
  • node-red

I cannot establish any connection at all either via mqtt or ws protocols.

Conclusion: mosquitto is fine and all the other three are faulty in some way.

But I just don’t see how.

What other tests can I run to resolve this? All suggestions most gratefully received.

Ric

This indeed seems weird.
I do believe that there is an input difference from the client side between the option you are using.
Can you check or show an example for the set details?
Further, have you checked the broker logs? Every incoming client connection gets logged. So if these requests reach the broker, there will be some information about this connection process.
You can also add the highest logging to make this more verbose.

I thought about this overnight… Terminal sessions (pub/sub) are BOTH inside the container and can therefore communicate on port 1883 - that would be container port 1883.

A network port scan of the RPI device this morning shows port 1883 isn’t open - all others are. My guess is that the external port has not been exposed somehow, but I cannot see anything wrong in the compose file…

I didn’t understand your first paragraph: “difference between”…? and what “set”…? I am unfamiliar with this term.

I shall check the logs after lunch.

Thanks.

Further edit after lunch:

I can confirm only those pub/sub connections INSIDE the container are recorded in the logs; no external connection is recorded.

In the original post I recorded:

I’m really not sure this is correct anymore: my most significant suspicion is that the ~.conf file is not being loaded and therefore there is no listener on ports 1883 nor 9001 and that’s the reason why those external clients cannot connect. My next attention would therefore be: the volume directives aren’t working as expected…. The odd thing is that I’ve used the same constructs for years without issue, even with previous versions of mosquitto. The standard directory structure lies within $HOME and is (with the addition of the config subdir in the instance of mosquitto)

$HOME
docker
compose
mosquitto
compose.yaml (file)
config
mosquitto.conf (file)
volumes
mosquitto
<created directories/files>

Is mosquitto expecting the volumes/mosquitto/config/mosquiito.config file to pre-exist rather than the one in compose? If so, what should the owner/permissions be? Dunno. I need to do more tests.

An interesting journey…

I have spent some time trying various options. The first success was running mosquitto from the CLI. The compose directives ought to be the same. But they are not and this is the first time I’ve had to adapt the installation compose file.

To be clear: I have always placed a compose.yaml file in the structure outlined in my previous post together with any configuration files:

docker/compose/<application_name>/<subfolders & files etc..etc>

Volumes are then created by the script and populated accordingly. Not so, it seems, with mosquitto.

With my previous compose/config setup, mosquitto.conf is definiely NOT picked up. Client pub/sub via a terminal works and gives the illusion all is well, but this is NOT a definitive test because the communication is made by an exec into the container. Similar tests, outlined in my first post, are also misleading if they rely on connections on the same machine. Whilst netstat on the same machine (my first post) shows the ports are up, they are definitely closed to all other devices on the network. This is a classic symptom of the listener directives in mosquitto.conf not being picked up for //mqtt and //ws.

The solution was to prepare the volumes ahead of “docker compose up -d” and load mosquitto.conf in the /docker/volumes/mosquitto/config folder. Then and only then have I been able to get mosquitto to function correctly. Messages sent between MQTX ←→ MQTX-explorer ←→ Node-Red on both //mqtt and //ws protocols are now fine. Log files are available to view in $HOME/docker/volumes/mosquitto/log/ . Mosquitto IS working correctly.

Not all is as I want it: Portainer usefully shows log files (I use it only as a lazy convenience for starting/stopping/removing/inspecting logs/confirming parameters, etc; installation is always via CLI), but does not show the usual startup messages nor any client connections, only some cryptic message on a single Iine I am unable to resolve:

No log line matching the ‘’ filter

So I haven’t quite got the configuration right.

I’m calling it a day at this point because I don’t understand enough to resolve the residual issues; someone else may see this and say “Hey, hell yes…”. But until that moment, this is far as I can go.