[英]Connection refused when connecting from one docker container to another [same network]
Having a hard time debugging this.很难调试这个。 I have one container starting an MQTT server and another Python container trying to connect.我有一个容器启动 MQTT 服务器,而另一个 Python 容器试图连接。
Using docker-compose to orchestrate and have set up a network and connected both containers to it.使用 docker-compose 编排并建立了一个网络并将两个容器连接到它。
Currently I can connect to the MQTT server independently with docker run and with docker-compose, but the Python containe for some reason cannot connect.目前我可以通过运行 docker 和 docker-compose 独立连接到 MQTT 服务器,但 Python 包含由于某种原因无法连接。
I'm thinking it may be a firewall issue?我在想这可能是防火墙问题?
In the main.py I'm printing the MQTT_HOST and MQTT_PORT and I can actually connect to those using a local Mosquitto client.在 main.py 中,我正在打印 MQTT_HOST 和 MQTT_PORT,我实际上可以使用本地 Mosquitto 客户端连接到那些。
docker-compose.yml 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()
You are trying to connect to localhost
which is going to be the container the python app is running in, not webserver
.您正在尝试连接到localhost
,它将成为 python 应用程序正在运行的容器,而不是webserver
。
Change the python to try to look up the address of webserver
rather than localhost
更改 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
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.