Assuming you’re sensible enough to be using a Docker Compose file, you can “install” Mosquitto with the following addition to your compose file:
mosquitto:
container_name: mosquitto
image: eclipse-mosquitto:2
restart: unless-stopped
ports:
- "1883:1883/tcp"
volumes:
- /data/mosquitto/config:/mosquitto/config
- /data/mosquitto/data:/mosquitto/data
- /data/mosquitto/log:/mosquitto/log
healthcheck:
test: ["CMD", "mosquitto_sub", "-u", "test", "-P", "test_user_password", "-h", "localhost", "-t", "$$SYS/broker/uptime", "-i", "docker_health_check", "-C", "1"]
interval: 1m
timeout: 10s
retries: 3
Then in /data/mosquitto/config you’ll want to create a config file. Mine for instance looks like:
persistence true
persistence_location /mosquitto/data/
password_file /mosquitto/config/pwfile
allow_anonymous false
log_dest file /mosquitto/log/mosquitto.log
listener 1883
And once you start the container you can create a user in mosquitto with a command like this:
docker compose exec mosquitto mosquitto_passwd -b /mosquitto/config/pwfile test test_user_password
Once you’ve created a user you need to restart mosquitto (restart the container)