The docker-compose.yml file includes a basic installation of Mosquitto on a Docker container. Clone the repository and follow the given steps below:
- Start container:
docker compose up -d. Three new directories (config,data,log) will be created. - Get into the relevant container:
docker exec -it mqtt sh. - Generate authentication file:
mosquitto_passwd -c /mosquitto/config/auth username. Replace theusernamewith your own. - Enter the password once prompt.
- Create a configuration file:
touch config/mosquitto.confwith the following configurations:
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
allow_anonymous false
password_file /mosquitto/config/pwfile
listener 1883
listener 9001
protocol websockets
- Restart Docker container:
docker compose restart - Test the connection using MQTTX or your preferred MQTT client.
- Fill up the username, password, and use
localhostas the Host withmqtt://prefix. - Create a new subscription on the client and send a message to the relevant topic to test.
If you prefer a video guide, head to the YouTube video that I published for everyone.