Before we start, make sure you have the following installed on your machine:
- Open your terminal and create a new directory called
- Change to the
- Create the
configfolder and the
password.txtfiles inside it (with the content as following):
mkdir config echo 'listener 1883 persistence true persistence_location /mosquitto/data/ log_dest file /mosquitto/log/mosquitto.log allow_anonymous false password_file /mosquitto/config/password.txt' > config/mosquitto.conf echo "admin:\$7\$101\$xuOfObQdv0Hi2p0A\$o+J3vzbpm0hekukYw73tT3fiB2Ogi1UCJgbzNyFA0GOgxAo79hqfTeXVr062KoD5nCphwV+1V/NxlMzwnV5Kvg==" > config/password.txt
- Create the
logfolder and the
mosquitto.logfile inside it:
mkdir log touch log/mosquitto.log
- Set the read and write permissions for all users for the following files:
chmod o+w log/mosquitto.log chmod 766 -R config
- Create the
docker-compose.ymlfile with the following content:
version: '3.8' networks: mqtt-net: driver: bridge ipam: driver: default config: - subnet: 126.96.36.199/24 services: mqtt-broker: image: eclipse-mosquitto:latest user: mosquitto volumes: - type: bind source: ./config/ target: /mosquitto/config/ read_only: false - type: bind source: ./log/ target: /mosquitto/log/ read_only: false - type: volume source: data target: /mosquitto/data/ ports: - target: 1883 published: 1883 protocol: tcp mode: host - target: 9001 published: 9001 protocol: tcp mode: host networks: mqtt-net: ipv4_address: 188.8.131.52 mqtt-pub: image: eclipse-mosquitto:latest command: sh -c "mosquitto_pub -h mqtt-broker -t test -m 'Hello World' -u admin -P password" depends_on: - mqtt-broker networks: mqtt-net: ipv4_address: 184.108.40.206 mqtt-sub: image: eclipse-mosquitto:latest command: sh -c "mosquitto_sub -h mqtt-broker -t test -u admin -P password" depends_on: - mqtt-broker networks: mqtt-net: ipv4_address: 220.127.116.11 volumes: data: name: "mqtt-broker-data"
Note that we have assigned a static IP address of
18.104.22.168 to the
- Build and start the containers by running the following command:
docker-compose up --build
This should bring up 3 services namely mqtt-broker, mqtt-pub, mqtt-sub. And the mqtt-pub service will immediatly exit since it just exits after publishing a message.
The default username and password for mqtt-broker is
- We can now publish messages using the mqtt-pub service using the docker compose.
docker compose run mqtt-pub sh -c "mosquitto_pub -h mqtt-broker -t test -m 'Hello World' -u admin -P password" docker compose run mqtt-pub sh -c "mosquitto_pub -h mqtt-broker -t test -m 'Message 2' -u admin -P password" docker compose run mqtt-pub sh -c "mosquitto_pub -h mqtt-broker -t test -m 'Message 3' -u admin -P password"
And we shall be able to see the output as following:
mqtt-mqtt-sub-1 | Hello World mqtt-mqtt-sub-1 | Message 2 mqtt-mqtt-sub-1 | Message 3
- To change the password of admin user (after changing the password the service needs to be restarted):
docker compose exec mqtt-broker mosquitto_passwd -b /mosquitto/config/password.txt admin NEWPASSWORD
Understanding the command:
docker: This is the command used to interact with Docker, a containerization platform used to run applications in isolated environments.
compose: This is an option used with the
dockercommand to run a multi-container Docker application using a Compose file.
exec: This is an option used with the
docker-composecommand to execute a command inside a running container.
mqtt-broker: This is the name of the service in the Compose file that corresponds to the MQTT broker container.
mosquitto_passwd: This is the command to set or update a password in the password file used by the Mosquitto MQTT broker.
-b: This is an option for the
mosquitto_passwdcommand that specifies that the next argument is the password to be added or updated.
/mosquitto/config/password.txt: This is the path to the password file in the Mosquitto container.
admin: This is the username to which the password will be added or updated.
NEWPASSWORD: This is the new password that will be set for the
adminuser in the Mosquitto password file.
In summary, this command sets or updates the password for the
admin user in the Mosquitto password file located at
/mosquitto/config/password.txt with the new password
NEWPASSWORD, inside the running
mqtt-broker container in a Docker Compose environment.
Restart the container:
docker compose restart mqtt-broker
- Since we changed the admin password, we no longer will be able to start the mqtt-pub service as
docker compose up mqtt-sub. We need to start it using the following command:
docker compose run mqtt-sub sh -c "mosquitto_sub -h mqtt-broker -t test -u admin -P CHANGED_PASSWORD"
Now we can publish a message (using another terminal) and see that being refelceted in the mqtt-sub container service.
docker compose run mqtt-pub sh -c "mosquitto_pub -h mqtt-broker -t test -m 'Hello World' -u admin -P CHANGED_PASSWORD"