无法部署Python服务器与Mosquitto MQTT Broker的问题求助
无法部署Python服务器与Mosquitto MQTT Broker的问题求助
大家好,我最近尝试用Docker Compose部署一个连接Mosquitto MQTT Broker的Python服务器,但折腾了好久都没能成功跑起来,想来这里求助各位大佬。
下面是我用到的所有配置文件和代码,麻烦帮忙看看哪里可能出问题了:
我的Dockerfile
# Use the official python image FROM python:3.12 # set the working directory WORKDIR /app # copy the current directory contents into the container at /app COPY . /app # install libgl RUN apt update && apt install -y libgl1-mesa-glx && rm -rf /var/lib/apt/lists/* # install the required packages RUN pip install -r requirements.txt # run the server script CMD ["python", "server.py"]
docker-compose.yml 配置
version: '3' services: mqtt2: image: eclipse-mosquitto:latest ports: - "1883:1883" - "9001:9001" volumes: - ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro server: build: . depends_on: - mqtt2 environment: - MQTT_BROKER=mqtt2 links: - mqtt2
mosquitto.conf 配置文件
listener 1883 allow_anonymous true listener 9001 protocol websockets
我的server.py代码(未完成)
import paho.mqtt.client as mqtt # 后续的MQTT连接逻辑还在写,但目前部署阶段就遇到了问题——Python服务没法成功连接到MQTT Broker,要么报连接超时,要么启动后没有任何响应
我已经在docker-compose里给server服务加了depends_on依赖mqtt2,也设置了环境变量指定Broker地址为mqtt2,甚至加了links,但还是没解决问题。想问问各位有没有什么排查的方向,或者我哪里的配置写错了?
备注:内容来源于stack exchange,提问作者Ali Kariminejad
相关产品推荐
相关产品推荐

