首页 > 解决方案 > Mosquitto 的 Docker-compose 健康检查

问题描述

我使用密码文件设置 mosquitto 密码

volumes:
  - /password:/mosquitto/config

如何在 docker-compose 中添加健康检查?我尝试了此处提供的以下解决方案 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

此外,我尝试了其他几个选项,但他们要求我传递用户名和密码。我不能使用这个密码文件吗?

更新: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

标签: mosquitto

解决方案


在推送时,您可以使用 MQTT over Websockets 作为协议启用侦听器,然后使用基本的 curl get 请求来检查代理是否已启动。

例如将其添加到 mosquitto.conf

listener 8080 127.0.0.1
protocol websockets

和健康检查之类的

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

原始 HTTP GET 请求应该在不需要验证的情况下完成。

另一个选项是重新启用匿名用户并为匿名用户添加只读访问权限以$SYS/#使用 acl 文件 ( acl_file )访问主题模式


推荐阅读