Search code examples
dockermqtt

mqtt server in docker - a way to run the <mosquitto_passwd -U> from DockerCompose.yaml


finally got mqtt running in Docker, but I want to use uid/pwd. Got all of it working, by:

  1. 1st using a mosquitto.conf file that does not ask for the passwd file,
  2. firing up the mqtt service via a DockerCompose.yaml from the mqtt image 3. have created,
  3. then logging into the service with docker exec -it containerid sh,
  4. I then, in the service, run the command mosquitto_passwd -U passwdfile to encrypt the open passwords I have in the file;
  5. Then I stop the stack of the docker service, change the mosquitto.conf for the location of the passwordfile, stsrtup the stack and the service again, then it works.

If I do not do it like this, the mqtt container exits, and says the passwd file is not encrypted correctly.

So, is their a way to maybe run the encrypt command in the docker container before the mqtt server expects it?


Solution

  • Another alternative is to run mosquito_passwd inside the container, i.e. on the container shell. Here is what worked for me (note that I have the config directory persisted outside the container):

    1. Create your mosquitto_passwords file in the config directory

    2. Enter the container

    $ docker exec -it mosquitto sh
    

    (Where mosquitto is the name of the mosquitto container)

    1. Navigate to your password file
    / $ cd mosquitto/
    /mosquitto $ ls
    config  data    log
    /mosquitto $ cd config/
    /mosquitto/config $ ls
    mosquitto.conf       mosquitto_passwords
    
    1. Run mosquitto_passwd to hash your password file
    /mosquitto/config $ mosquitto_passwd -U mosquitto_passwords
    

    If you get an error like

    Error: Unable to open password file /mosquitto/config/mosquitto_passwords. Permission denied.
    

    you need to make sure that your password file and the config directory is accessible to the container-user. To find out which user that is do

    /mosquitto/config $ whoami
    whoami: unknown uid 1000
    

    So in this case you can do

    $ sudo chown 1000 config
    $ sudo chown 1000 config/mosquitto_passwords
    

    After that,

    /mosquitto/config $ mosquitto_passwd -U mosquitto_passwords
    

    should work fine and you're done.

    1. Exit the container
    /mosquitto/config $ exit
    

    Don't forget to point your mosquitto.conf at your new password file.