I've got two programs that are supposed to communicate via MQTT. One starts up a broker process and listens for messages from the other. When run locally, this all works, but inside Docker it doesn't. The Docker compose file has this for the ports of the mqtt container:
ports:
- "1883:1883"
I have also tried this, which didn't make any difference:
expose:
- "1883"
So it should just be the default port exposed. Within that container, the mosquitto_pub command can hit that port for "localhost" without error, but if the IP address assigned to the container or the name of the container is used for that command instead of localhost, the result is "Connection refused". Other containers can't use localhost for such a command (at least in Docker, this part works when run directly on my local machine), but if they use the IP/container name they also get connection refused.
A completely invalid host name will result in a different failure message, so I know at least the name is correct. Additionally, the mosquitto broker produces the message "Error: Cannot assign requested address" when started on localhost (or 0.0.0.0) in a Docker container, but not outside, and still continues running otherwise seemingly without error.
EDIT: The mosquitto.conf file is the default, without modifications.
The default mosquitto config only listens on localhost
so can not be connected from anything outside the container
You need to create a local file called mosquitto.conf
with the following content:
listener 1883
allow_anonymous true
and mount it on /mosquitto/config/mosquitto.conf
volumes:
- "./mosquitto.conf:/mosquitto/config/mosquitto.conf"