Skip to content

Instantly share code, notes, and snippets.

@rwcitek
Last active July 10, 2023 04:24
Show Gist options
  • Save rwcitek/36841f672e11a07a15592dda9cdc9634 to your computer and use it in GitHub Desktop.
Save rwcitek/36841f672e11a07a15592dda9cdc9634 to your computer and use it in GitHub Desktop.
An example pub/sub using mosquitto in Docker
# === Subscriber and publisher in single container, eclipse broker
# remove any existing container
docker container stop mosquitto ; docker container rm mosquitto
# start the instance in the background ( and create an image, if needed )
docker image list -a | grep -q mosquitto &&
docker container run -d --name mosquitto mosquitto sleep inf || {
docker container run -d --name mosquitto ubuntu:22.04 sleep inf
# install mosquitto and suite of tools
docker container exec -i mosquitto /bin/bash << 'eof'
export DEBIAN_FRONTEND=noninteractive
apt-get update &&
apt-get install -y --no-install-recommends \
mosquitto \
mosquitto-clients \
curl \
iputils-ping \
less \
net-tools \
telnet \
tree \
vim \
;
eof
# create image
docker container commit mosquitto mosquitto
}
# run a subscriber
docker container exec -i mosquitto /bin/bash << 'eof'
# run a subscriber
mosquitto_sub \
--host mqtt.eclipseprojects.io \
--topic CNMI/IoT |
tee /tmp/sub.log
eof
# run a publisher
docker container exec -i mosquitto /bin/bash << 'eof'
# run a publisher, publishing to mqtt.eclipseprojects.io
for i in {1..10} ; do
mosquitto_pub \
--host mqtt.eclipseprojects.io \
--topic CNMI/IoT \
--message "$( date --iso=s ) MQTT Client"
sleep 1
done
eof
# examine log file of messages
docker container exec mosquitto cat -n /tmp/sub.log
# work inside container
docker container exec -it mosquitto /bin/bash
# === Separate subscriber and publisher container, eclipse broker
# Create a subscriber
docker container run --rm --name mqtt_sub -i mosquitto /bin/bash << 'eof'
mosquitto_sub \
--host mqtt.eclipseprojects.io \
--topic CNMI/IoT |
tee /tmp/sub.log
eof
# Create a publisher
docker container run --rm --name mqtt_pub -i mosquitto /bin/bash << 'eof'
sleep 3
for i in {1..10} ; do
mosquitto_pub \
--host mqtt.eclipseprojects.io \
--topic CNMI/IoT \
--message "$( date --iso=s ) MQTT Client"
sleep 1
done
eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment