I am tring to run a mosquitto broker and two Rust applications that comunicate between over mqtt.
My docker-compose.yml
version: '2.1'
networks:
mqtt:
services:
mqtt-broker:
build: ./mqtt-broker
ports:
- "1883:1883"
networks:
mqtt:
mqtt-pub:
build: ./mqtt-pub
depends_on:
- mqtt-broker
networks:
mqtt:
mqtt-sub:
build: ./mqtt-sub
depends_on:
- mqtt-broker
networks:
mqtt:
The Docker.template
of each application looks like:
# Rust as the base image
FROM rust:slim-bookworm as builder
ENV UDEV=1
ENV RUST_BACKTRACE=1
# Create a new empty shell project
RUN USER=root cargo new --bin mqtt-sub
WORKDIR /usr/src/mqtt-sub
#stall dependencies
RUN apt-get update && apt-get install -y \
git \
cmake \
libudev-dev \
build-essential
# Copy our manifests
COPY ./Cargo.* ./
RUN mkdir ./src && echo 'fn main() { println!("Dummy!"); }' > ./src/main.rs
# Build only the dependencies to cache them
RUN cargo build --release --bin real && rm src/*.rs
# Now that the dependency is built, copy your source code
COPY ./src ./src/
RUN touch -a -m ./src/main.rs
# Build for release.
RUN cargo build --release --bin real --target-dir .
CMD [ "./release/real" ]
I use localhost
as host and 1883
port.
I am getting this error:
[mqtt-pub] Error in event loop: Io(Os { code: 111, kind: ConnectionRefused, message: "Connection refused" })
My guess is that the containers cannot connect to the mosquitto broker.
Hello, are you able to successfully ping between the two containers?
Also note that if you are using Mosquitto 2.x then anonymous connections are disabled by default - check out their documentation for how to configure that differently if you need to.
I cannot ping between containers.
The Docker.template
of mqtt-broker
is:
FROM arm64v8/eclipse-mosquitto:2.0
COPY ./mosquitto.conf /mosquitto/config/mosquitto.conf
CMD ["/usr/sbin/mosquitto", "-c", "/mosquitto/config/mosquitto.conf"]
I am using a custom mosquitto.conf
file for settin up the broker:
# Maximum connections (-1 for unlimited)
max_connections -1
# Standart MQTT connection
listener 1883
protocol mqtt
allow_anonymous true
However if I run mosquitto -v
in the mqtt-broker
container I get this:
Spawning shell...
/ # mosquitto -v
1723189515: mosquitto version 2.0.18 starting
1723189515: Using default config.
1723189515: Starting in local only mode. Connections will only be possible from clients running on this machine.
1723189515: Create a configuration file which defines a listener to allow remote access.
1723189515: For more details see https://mosquitto.org/documentation/authentication-methods/
1723189515: Opening ipv4 listen socket on port 1883.
1723189515: Error: Address in use
1723189515: Opening ipv6 listen socket on port 1883.
1723189515: Error: Address not available
It might help to resolve the communication between containers first. Can you post your docker-compose file? Perhaps there is a networking conflict of some kind.
The docker-compose.yml
is:
version: '2.1'
networks:
mqtt:
services:
mqtt-broker:
build: ./mqtt-broker
restart: always
ports:
- "1883:1883"
networks:
mqtt:
mqtt-pub:
build: ./mqtt-pub
restart: always
depends_on:
- mqtt-broker
networks:
mqtt:
mqtt-sub:
build: ./mqtt-sub
restart: always
depends_on:
- mqtt-broker
networks:
mqtt:
I am able to ping containers between them.
After pining the containers between them, I used the container name as host for MQTT.
let mut mqttoptions = MqttOptions::new("publisher", "mqtt-broker", 1883);
1 Like