-
拉取镜像
docker pull eclipse-mosquitto
-
创建目录
mkdir mqtt
cd mqtt
mkdir config
mkdir data
mkdir log
-
创建配置文件
cd mqtt/config
touch mosquitto.conf
touch passwd
vim mosquitto.conf
点击查看代码
listener 1883
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
password_file /mosquitto/config/userpass.conf
per_listener_settings false
allow_anonymous false
- 创建docker-compose工具配置文件
touch docker-compose.yml
vim docker-compose.yml
点击查看代码
services:
mqtt:
container_name: mosquitto
image: eclipse-mosquitto
volumes:
- ./config:/mosquitto/config
- ./data:/mosquitto/data
- ./log:/mosquitto/log
ports:
- 1883:1883
- 9001:9001
-
创建容器并启动
cd mqtt
docker compose up
-
创建用户
chown root /mosquitto/config/passwd
docker exec -it mosquitto sh -c "mosquitto_passwd -c /mosquitto/config/passwd 自定义用户名"
按照提示输入密码 -
重启服务
docker restart mosquitto
-
官网下载MQTT X客户端,安装并测试连接。