1.docker pull
docker pull eclipse-mosquitto
2. View the mirror image
docker images
3. Create a configuration directory
mkdir -p /mosquitto/config
mkdir -p /mosquitto/data
mkdir -p /mosquitto/log
4. Create a configuration file, most of the tutorials are missing: listener 1883
vi /mosquitto/config/mosquitto.conf
write the following
persistence true
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log
listener 1883
allow_anonymous false #关闭匿名模式
password_file /mosquitto/config/pwfile.conf #指定密码文件
5. Authorize the directory
chmod -R 755 /mosquitto
chmod -R 777 /mosquitto/log #日志目录要最大权限
6. Create and run the script mosquitto.sh
docker run -it --name=mosquitto --privileged -p 1883:1883 -p 9001:9001 -v /mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf -v /mosquitto/data:/mosquitto/data -v /mosquitto/log:/mosquitto/log -d eclipse-mosquitto
If [ Error: Unable to open log file /mosquitto/log/mosquitto.log for writing.]
is displayed, delete the existing log file and create a new one and authorize it to start again.
7. View the container
docker pscd ..
8. Enter the container
docker exec -it 3fdbb2bc353c sh
9. Generate password
#For passworf_file, you can copy a template, or create an empty file
touch /mosquitto/config/pwfile.conf
chmod -R 755 /mosquitto/config/pwfile.conf
Use the mosquitto_passwd command to create a user, the first test is the username, and the second test2022 is the password
mosquitto_passwd -b /mosquitto/config/pwfile.conf test test2019
Exit the container
10. Restart the mqtt service
docker restart 容器id
11. Use MQTT.fx to connect, the server must open the corresponding port