2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DockerでMosquittoを起動

Last updated at Posted at 2021-11-15

はじめに

MQTTブローカーのmosquittoをDockerで起動しようとしたらちょっとだけ躓いたのでメモ。

躓いたやり方

デフォルトで起動したMQTTブローカーが欲しかっただけなのでここを参考に、

$ docker run -it -d -p 1883:1883 -p 9001:9001 eclipse-mosquitto

でmosquittoを起動し、paho-mqttを使って書いた以下のようなpublisher, subscriberを起動しましたがうまくつながりませんでした。

publisher.py
from paho.mqtt import client as mqtt_client
import time


CLIENT_ID = 'publisher'
BROKER = 'localhost'
PORT = 1883
TOPIC = 'pyjackal/mqtt'


def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to MQTT Broker!")
    else:
        print("Failed to connect, return code %d\n", rc)


def run():
    client = mqtt_client.Client(CLIENT_ID)
    client.on_connect = on_connect
    client.connect(BROKER, PORT)
    client.loop_start()
    count = 0
    while True:
        time.sleep(1)
        msg = f"message{count}"
        result = client.publish(TOPIC, msg)
        status = result[0]
        if status == 0:
            print(f"Send `{msg}`")
        else:
            print(f"Failed to send message to {TOPIC}")
        count += 1


if __name__ == '__main__':
    run()
subscriber.py
from paho.mqtt import client as mqtt_client


CLIENT_ID = 'subscriber'
BROKER = 'localhost'
PORT = 1883
TOPIC = 'pyjackal/mqtt'


def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to MQTT Broker!")
    else:
        print("Failed to connect, return code %d\n", rc)


def on_message(client, userdata, msg):
    print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")


def run():
    client = mqtt_client.Client(CLIENT_ID)
    client.on_connect = on_connect
    client.connect(BROKER, PORT)
    client.subscribe(TOPIC)
    client.on_message = on_message
    client.loop_forever()


if __name__ == '__main__':
    run()

docker logsでlogを見ると、以下のようなログが出ていました

$ docker logs quizzical_wu
1636985393: mosquitto version 2.0.13 starting
1636985393: Config loaded from /mosquitto/config/mosquitto.conf.
1636985393: Starting in local only mode. Connections will only be possible from clients running on this machine.
1636985393: Create a configuration file which defines a listener to allow remote access.
1636985393: For more details see https://mosquitto.org/documentation/authentication-methods/
1636985393: Opening ipv4 listen socket on port 1883.
1636985393: Opening ipv6 listen socket on port 1883.
1636985393: Error: Address not available
1636985393: mosquitto version 2.0.13 running
$

うまくいったやり方

ログやこちらに記載されている通り、mosquitto2.0からデフォルトだとlocalhost以外からのアクセスを禁止していたからでした。
以下のような設定ファイルを用意し、設定ファイルを指定してコンテナを起動するとうまくいきました。

mosquitto.conf
listener 1883  # 0.0.0.0:1883でlisten
allow_anonymous true  # 認証なしでの接続を許可
docker run -it -d -p 1883:1883 -p 9001:9001 -v $PWD/mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?