Search code examples
mosquitto

Docker-compose health check for Mosquitto


I setup mosquitto password using a password file

volumes:
  - /password:/mosquitto/config

How can I add healthcheck in docker-compose? I tried the below solution provided here Script to check mosquitto is healthy

healthcheck:
  test: ["CMD-SHELL", "timeout -t 5 mosquitto_sub -t '$$SYS/#' -C 1 | grep -v Error || exit 1"]
  interval: 10s
  timeout: 10s
  retries: 6

Also, I tried a couple of other options but they are asking me to pass username and password. Can't I use this password file?

update: mosquitto.conf

allow_anonymous false
password_file /mosquitto/config/pwfile
port 1883
listener 9001
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

Solution

  • At a push you could enable listener with MQTT over Websockets as the protocol and then use a basic curl get request to check it the broker is up.

    e.g. add this to the mosquitto.conf

    listener 8080 127.0.0.1
    protocol websockets
    

    and a health check something like

    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080" ]
      interval: 10s
      timeout: 10s
      retries: 6
    

    The raw HTTP GET request should complete without needing to authenticate.

    The other option is re-enable anonymous users and to add readonly access to the anonymous user to access the $SYS/# topic pattern using a acl file (acl_file)