# docker mosquitto mqtt server



```
git clone https://github.com/toke/docker-mosquitto.git
sudo mkdir -p /docker/mqtt/log /docker/mqtt/config /docker/mqtt/data
sudo touch /docker/mqtt/log/mosquitto.log
sudo chmod 666 /docker/mqtt/log/mosquitto.log
cd docker-mosquitto/config/
sudo cp -a * /docker/mqtt/config/
sudo docker run -itd -p 1885:1883 -p 9001:9001 -v /docker/mqtt/config:/mqtt/config -v /docker/mqtt/log:/mqtt/log -v /docker/mqtt/data/:/mqtt/data/ --name mqtt toke/mosquitto
```

發送測試

```
docker exec mqtt mosquitto_pub -t hello -m "helloworld"
```

![](https://529150212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lh81JtaIQ84bwQqz_Wh%2F-LkasUtg5GxSGzYKgLF9%2F-Lkau-BQasXl0JPijxN4%2Fimage.png?alt=media\&token=b4606dc3-7c49-4cfa-8e06-cb8800633116)

## **使用者認證**

進入容器建立帳號密碼

![](https://529150212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lh81JtaIQ84bwQqz_Wh%2F-Lkb8hievS8RhBqEhGgE%2F-Lkb8jjFAu8yi7dloZH0%2Fimage.png?alt=media\&token=8d968118-dfc5-47b5-87bd-7b0c9e1cbb21)

```
# Place your local configuration in /mqtt/config/conf.d/
  
pid_file /var/run/mosquitto.pid

persistence true
persistence_location /mqtt/data/

user mosquitto
#-----------need user auth---------
password_file /mqtt/config/passwd
allow_anonymous false
#---------------------------------
# Port to use for the default listener.
port 1883


log_dest file /mqtt/log/mosquitto.log
log_dest stdout

include_dir /mqtt/config/conf.d
```

mqtt 測試

docker exec mqtt mosquitto\_pub -t hello -m "helloworld" -u user -P password

![](https://529150212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lh81JtaIQ84bwQqz_Wh%2F-LkbDIb_m_HLnTR6DPvk%2F-LkbDxTF6TELH0DrN-NN%2Fimage.png?alt=media\&token=2fd889c3-d380-4488-b6e0-df3f9a1b0fc1)

