很难调试它。我有一个启动MQTT服务器的容器,另一个试图连接的Python容器。
使用docker-compose进行协调,并建立了一个网络并将两个容器都连接到该网络。
当前,我可以使用docker run和docker-compose独立连接到MQTT服务器,但是由于某些原因Python容器无法连接。
我认为这可能是防火墙问题?
在main.py中,我正在打印MQTT_HOST和MQTT_PORT,实际上我可以使用本地Mosquitto客户端连接到这些服务器。
docker-compose.yml
version: "3.3"
services:
webserver:
build: ./webservice/server
ports:
- 3001:3001
- 3002:3002
networks:
- project-network
command: npm run start
inferemce:
build: ./inference
ports:
- 3003:3003
networks:
- project-network
depends_on:
- webserver
restart: on-failure
command: ["./wait-for-it.sh", "webserver:3001", "--", "python", "main.py"]
networks:
project-network:
driver: bridge
main.py
import socket
import paho.mqtt.client as mqtt
HOSTNAME = socket.gethostname()
IPADDRESS = socket.gethostbyname('localhost')
MQTT_HOST = IPADDRESS
MQTT_PORT = 3001
MQTT_KEEPALIVE_INTERVAL = 60
def connect_mqtt():
print('host', MQTT_HOST)
print('port', MQTT_PORT)
client = mqtt.Client()
client.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
return client
def main():
client = connect_mqtt()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
您正在尝试连接到localhost
,而这将是运行python应用程序的容器,而不是webserver
。
更改python以尝试查找webserver
而不是localhost
的地址
import socket
import paho.mqtt.client as mqtt
HOSTNAME = socket.gethostname()
IPADDRESS = socket.gethostbyname('webserver')
MQTT_HOST = IPADDRESS
MQTT_PORT = 3001
MQTT_KEEPALIVE_INTERVAL = 60
...