安装
bash
cd /data/mqqt/
目录准备
mkdir /data/mqqt/mosquitto/config/
#vim Dockerfile
bash
# 使用官方Eclipse Mosquitto镜像作为基础镜像
FROM eclipse-mosquitto:latest
# 复制配置文件到容器内
COPY mosquitto.conf /data/mqqt/mosquitto/config/mosquitto.conf
# 暴露默认的MQTT端口和WebSocket端口
EXPOSE 1883
EXPOSE 9001
# 运行Mosquitto服务
CMD ["/usr/sbin/mosquitto", "-c", "/data/mqqt/mosquitto/config/mosquitto.conf"]
#vim mosquitto.conf
#添加你的配置内容,例如:
bash
listener 1883
allow_anonymous true
listener 9001
protocol websockets
ip
bash
192.168.3.100
构建Docker镜像
bash
docker build -t custom-mosquitto .
运行Docker容器
使用以下命令运行刚才构建的Docker镜像,并确保容器对外开放端口:
bash
docker run -d --name mosquitto -p 1883:1883 -p 9001:9001 custom-mosquitto
测试MQTT代理
bash
sudo apt-get install mosquitto-clients
使用mosquitto_pub和mosquitto_sub测试发布和订阅消息:
topic
test/topic
测试发布下消息
bash
mosquitto_pub -h 192.168.3.100 -t test/topic -m "Hello MQTT"
测试接收消息
bash
mosquitto_sub -h 192.168.3.100 -t test/topic