docker安装mosquitto
拉取镜像
版本:2.0.14
docker pull eclipse-mosquitto:latest
在本地新建目录
sudo mkdir /home/iotts/mosquitto
cd /home/iotts/mosquitto
sudo mkdir config
sudo mkdir data
sudo mkdir log
touch config/pwfile.conf
新建配置文件
vi ./config/mosquitto.conf
粘贴如下内容: 注意:此处路径都是docker容器中的相对路径,不是自己本地的路径
persistence true
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log
log_dest stdout
listener 1883 //不加这一句会在新版本2.x.x中报错:Error: Address not available
修改目录权限
chmod -R 755 /home/iotts/mosquitto
chmod -R 777 /home/iotts/mosquitto/log
创建并启动容器
sudo docker run -it --name=mosquitto --privileged \
--restart always \
-p 1883:1883 -p 9001:9001 \
-v /home/iotts/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf \
-v /home/iotts/mosquitto/config/pwfile.conf:/mosquitto/config/pwfile.conf \
-v /home/iotts/mosquitto/data:/mosquitto/data \
-v /home/iotts/mosquitto/log:/mosquitto/log \
-d eclipse-mosquitto
- -d:在后台运行容器
- –name:容器名
- -v:将容器内的目录映射到容器外
- -p:端口映射
设置密码
在 /home/iotts/mosquitto/config/mosquitto.conf 中继续添加
# 关闭匿名模式
allow_anonymous false
# 指定密码文件
password_file /mosquitto/config/pwfile.conf
进入容器
docker exec -it mosquitto sh
使用 mosquitto_passwd 命令创建用户
mosquitto_passwd -b /mosquitto/config/pwfile.conf username(此处为用户名) password(此处为密码)
重启容器
docker restart mosquitto