How to fix mosquitto_passwd overwriting all other users in the password file

Problem:

When running mosquitto_passwd like this:

docker-compose exec mosquitto mosquitto_passwd /mosquitto/conf/mosquitto.passwd myuser

or with the -c parameter:

docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/conf/mosquitto.passwd myuser

the user is created but all other users who where previously listed in the file are deleted.

Solution:

Create the first user using the -c flag in order to create the file if it does not exist

docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/conf/mosquitto.passwd firstuser

Then create additional users using -b (batch mode),which allows you to specify the password on the command line:

docker-compose exec mosquitto mosquitto_passwd -b /mosquitto/conf/mosquitto.passwd seconduser shoaCh3ohnokeathal6eeH2marei2o

When using -b, old users will not be deleted.