docker-compose部署mosquitto 2.0以上版本

没有的目录自行创建即可

  • 进入/home/docker/mosquitto

  • 编辑或创建 vi docker-compose.yml

version: '3'
networks:
  apg-net:
    external: true
services:
  apg-mosquitto-service:
    image: eclipse-mosquitto:2.0.14
    hostname: apg-mosquitto-service
    container_name: apg-mosquitto-service
    restart: always
    networks:
      - apg-net
    ports:
      - 11883:1883
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2000M
        reservations:
          memory: 100M
    volumes:
      - ./config:/mosquitto/config
      - ./password:/mosquitto/password
      - /home/dataVolume/mosquitto/data:/mosquitto/data
      - /home/dataVolume/mosquitto/log:/mosquitto/log
  • 创建mqtt配置目录和修改配置文件
[root@localhost mosquitto]# cd config/
[root@localhost config]# ls
mosquitto.conf
[root@localhost config]# vi mosquitto.conf 

#主要修改以下几项
persistence true
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log

listener 1883/0.0.0.0

allow_anonymous false

password_file /mosquitto/password/pwfile
  • 生成账号密码文件
#在配置账号密码前可以先启动容器,用容器内的命令生成账密文件
#进入到容器内部,执行命令
mosquitto_passwd -c /mosquitto/password/pwfile admin
#会出现两次输入密码的情况,输入admin即可
cat /mosquitto/password/pwfile 
admin:$7$101$iik9chuGw+s7F7GB$+xz6MnptTkxAoIZ1BCbkwJQIknYLsA89fy7q8NnLz2plTC5Rzpu9YI6A9l0CHdqQL2UUC3Q1Y7OA1hPyjxqdOA==
  • 停止容器,然后重新启动
docker-compose down
docker-compose --compatibility up -d
#使用mqtt连接工具测试连接以及订阅和发送消息 没问题就可以了

发表评论