I. Introduction
Due to work needs, mqtt server needs to be deployed for testing.
2. Technical summary
- The relevant images on dockerhub can be directly pulled and available. After checking,
eclipse-mosquitto
there is official certificationOFFICIAL
, so I use this version2.0.15
. - First start the container, obtain the configuration file, set the user and password, and generate the account file.
- Modify the configuration and attach the account file.
- Use client testing.
3. Practice
download
Order:
docker pull eclipse-mosquitto:2.0.15
Get configuration
Get the configuration file and create a password:
docker run --rm -it -v /tmp/config:config/mqtt --rm --name mqtt eclipse-mosquitto:2.0.15 sh
cd /tmp/config/
cp /mosquitto/config/mosquitto.conf .
touch /tmp/config/latelee.conf
mosquitto_passwd -b /tmp/config/latelee.conf latelee 123456
cat /tmp/config/latelee.conf
latelee:$7$101$8uztnbWciNtQhAB+$ElyWv714QaN8E/DOt9wzN/fRs9MdXtmpuq1xJifPKVfno1meGzCp4UnkZErM3WbaZtWIL/tKaxq72dXJ8oRwKw==
exit
If you want to create multiple users,
mosquitto_passwd -b /tmp/config/latelee.conf <用户> <密码>
just execute.
Change setting
Re-edit the file mosquitto.conf
:
persistence true
persistence_file mosquitto.db
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log
allow_anonymous false
password_file /mosquitto/config/latelee.conf
# 默认端口是 1883
port 1883
# 以下两行表示可以通过9001端口以websocket的方式连接mosquitto服务
#listener 9001
#protocol websockets
docker-compose configuration
Docker-compose.yaml file content:
ll-mqtt:
image: eclipse-mosquitto:2.0.15
container_name: ll-mqtt
hostname: ll-mqtt
# privileged: true
restart: always
#command: xxx
volumes:
- ./config/mqtt:/mosquitto/config
- ./mqttdata:/mosquitto/data
- ./mqttlog:/mosquitto/log
environment:
- TZ=Asia/Shanghai
ports:
- "11883:1883"
#- "19001:9001"
networks:
stdtoll-net:
ipv4_address: 172.18.200.30
start up:
docker-compose up -d ll-mqtt
Note: It seems to start slowly
4. Test
Use MQTTX
the tool to test, the address is here .
New link
An example link configuration is as follows:
)]
After entering the correct information, the connection is successful:
)]
Add new subscription
Use default topic
Send a message
Select subscription, enter Topic, and send. as the picture shows.
5. Multi-user testing
For the sake of simplicity, the same operations as the previous ones are omitted.
Add multiple user information:
mosquitto_passwd -b /tmp/config/latelee.conf latelee1 123456
# cat latelee.conf
latelee:$7$101$sBW5H73ll8re2i1F$JHHLDgQC8QN/Ig2y3dYw4QOVvAx1HyBSc94KW7y6E3rlCyDdeC909GhRjS5BW5RM4AARzO6xknhmDt1PvahAxA==
latelee1:$7$101$iZDWCtfmj9oby9BH$RoSTqv37knkc9Ow1O9f7N241KBv1TiLXRphg4xOnLQuHxEj5PQHcjLRkxj34Eqbjz6Cvvs2f1vgf9o+Kgg3qPg==
latelee2:$7$101$AcmF60V2ymIiGSJh$55FiXACOO7k9d20Pz4nt/mSTryVsQ+L+G59ihjDziz408JZWaIm41fisIu0AYITxZ+5bDgHM1IXPkjYos+AT6Q==
latelee3:$7$101$jCVWCbIsbRNuP+zv$IGdfF/E5LVWxRUSk3a+x5AXks9tFFbN3R6cqBnoq+aHpQxvzqrcn8fJtuSz2e3uokZyiPTosumcTx15cyeqkpA==
To be realized.
Reference: https://www.ifsvc.cn/posts/160
6. Advanced
The user of the mounted file will become 1883
because it is set in the image, but the system does not have its corresponding user name.
Users in the mirror:
cat /etc/passwd
root:x:0:0:root:/root:/bin/ash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
mosquitto:x:1883:1883:mosquitto:/var/empty:/sbin/nologin
The user mosquitto
's ID is 1883
. Directory permissions were changed in the image's startup script:
cat /docker-entrypoint.sh
#!/bin/ash
set -e
# Set permissions
user="$(id -u)"
if [ "$user" = '0' ]; then
[ -d "/mosquitto" ] && chown -R mosquitto:mosquitto /mosquitto || true
fi
exec "$@"
7. Reference
mqtt mirror: https://hub.docker.com/_/eclipse-mosquitto
attached
If no port is specified, it will prompt:
1679721126: mosquitto version 2.0.15 starting
1679721126: Config loaded from /mosquitto/config/mosquitto.conf.
1679721126: Starting in local only mode. Connections will only be possible from clients running on this machine.
1679721126: Create a configuration file which defines a listener to allow remote access.
1679721126: For more details see https://mosquitto.org/documentation/authentication-methods/
1679721126: Opening ipv4 listen socket on port 1883.
1679721126: Opening ipv6 listen socket on port 1883.
1679721126: Error: Address not available
1679721126: mosquitto version 2.0.15 running
Connection log:
1679721710: New connection from 192.168.28.11:62840 on port 1883.
1679721710: New client connected from 192.168.28.11:62840 as mqttx_5607984c (p5, c1, k60, u'latelee').
1679721767: New connection from 192.168.28.11:62868 on port 1883.
1679721767: New client connected from 192.168.28.11:62868 as mqttx_67e55882 (p5, c1, k60, u'latelee').
When using MQTTX
the tool to send a message, you must enter the correct Topic without wildcard characters such as "#".