Reputation: 1443
Having a hard time debugging this. I have one container starting an MQTT server and another Python container trying to connect.
Using docker-compose to orchestrate and have set up a network and connected both containers to it.
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.
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.
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()
Upvotes: 0
Views: 1914
Reputation: 59618
You are trying to connect to localhost
which is going to be the container the python app is running in, not webserver
.
Change the python to try to look up the address of webserver
rather than 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
...
Upvotes: 1