I’m running two dockers : one for the mqtt server using mosquitto the other using flask_mqtt
the flask container is receiving the CONNACK and is sending the subscribe to the broker but never get any SUBACK
however it manages to publish hello word to /home/mytopic (mqtt-explorer proof)
this is quite strange because it works without any problem outside of a docker
here is the docker compose :
JavaScript
x
25
25
1
services:
2
mqtt:
3
container_name: mqtt_container
4
image: eclipse-mosquitto:latest
5
networks:
6
- local_net
7
ports:
8
- "1883:1883"
9
volumes:
10
- ./MqttServer/:/mosquitto/config/:rw
11
flask:
12
container_name: flask_container
13
build:
14
context: Api
15
dockerfile: Dockerfile
16
networks:
17
- local_net
18
ports:
19
- "80:80"
20
21
networks:
22
local_net:
23
external: false
24
25
the flask dockerfile :
JavaScript
1
11
11
1
FROM python:latest
2
3
COPY requirements.txt /
4
RUN pip3 install -r /requirements.txt
5
6
COPY . /app
7
WORKDIR /app
8
9
# ENTRYPOINT ["gunicorn", "main:app", "-w", "1", "-b", "0.0.0.0:80"]
10
ENTRYPOINT ["python", "main.py"]
11
and the main.py
JavaScript
1
57
57
1
from flask import Flask
2
from flask_mqtt import Mqtt
3
from flask_restx import Api, Resource
4
5
app = Flask(__name__)
6
7
app.config['MQTT_BROKER_URL'] = "mqtt"
8
app.config['MQTT_BROKER_PORT'] = 1883
9
app.config['MQTT_KEEPALIVE'] = 60
10
11
mqtt = Mqtt(app)
12
13
api = Api(
14
app=app,
15
title='xxx',
16
description='yyy',
17
version='1.0.0',
18
)
19
20
21
@mqtt.on_connect()
22
def handle_connect(client, userdata, flags, rc):
23
if rc == 0:
24
print("connection established")
25
mqtt.subscribe('home/mytopic')
26
else:
27
print("connection error")
28
29
30
@mqtt.on_message()
31
def handle_mqtt_message(client, userdata, message):
32
print("i'm here")
33
data = dict(
34
topic=message.topic,
35
payload=message.payload.decode()
36
)
37
print(data)
38
39
40
@mqtt.on_log()
41
def handle_logging(client, userdata, level, buf):
42
print(buf)
43
44
45
@api.route('/test')
46
class Airflow(Resource):
47
def get(self):
48
mqtt.publish('home/mytopic', "hello world")
49
50
def post(self):
51
pass
52
53
54
if __name__ == '__main__':
55
app.run(host="0.0.0.0", port=80, debug=False, use_reloader=False)
56
57
Advertisement
Answer
Maybe one of the containers is missing an exposed port. In doubt, try linking them by adding a links:
tag in your compose like this :
JavaScript
1
19
19
1
version: "3.6"
2
services:
3
mqtt:
4
container_name: mqtt_container
5
image: eclipse-mosquitto:latest
6
ports:
7
- 1883:1883
8
volumes:
9
- ./MqttServer/:/mosquitto/config/:rw
10
links:
11
- flask
12
flask:
13
container_name: flask_container
14
build:
15
context: Api
16
dockerfile: Dockerfile
17
ports:
18
- 5000:5000
19