Why does my Python container does not communicate with my other applications?

Viewed 24

I have three python apps. They all use zeromq - one subscriber, one server and a proxy.They all work fine locally with the python *.py command but I want to dockerize them all and run them in docker containers. I have a dockerfile for the proxy and I docker built it and then ran it. It does not crash but when I turn the server and the client, they do not have communication between each other (nothing is sent/received) and this happen only when I run the docker proxy.

I am not sure what the problem is and how to resolve it.

Here is my code:

Proxy:

import zmq

def main():
    
    try:
        context = zmq.Context(1)
        # Socket facing clients
        frontend = context.socket(zmq.SUB)
        frontend.bind("tcp://*:5559")
        
        frontend.setsockopt_string(zmq.SUBSCRIBE, "")
        
        # Socket facing services
        backend = context.socket(zmq.PUB)
        backend.bind("tcp://*:5560")

        zmq.device(zmq.FORWARDER, frontend, backend)
    except Exception as e:
        print(e)
        print("bringing down zmq device")
    finally:
        pass
        frontend.close()
        backend.close()
        context.term()

if __name__ == "__main__":
    main()

Client:

import sys
import zmq

port = "5560"
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
print("Collecting updates from server...")
socket.connect ("tcp://localhost:%s" % port)
topicfilter = "9"
socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter)
for update_nbr in range(10):
    string = socket.recv()
    topic, messagedata = string.split()
    print(topic, messagedata)

Server:

import zmq
import random
import sys
import time

port = "5559"
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.connect("tcp://localhost:%s" % port)
publisher_id = random.randrange(0,9999)
while True:
    topic = random.randrange(1,10)
    messagedata = "server#%s" % publisher_id
    print("%s %s" % (topic, messagedata))
    socket.send_string("%d %s" % (topic, messagedata))
    time.sleep(1)

Dockerfile Proxy:

FROM python:3.10

RUN pip install pyzmq

RUN pip install Flask

ADD proxy.py /tmp/proxy.py

# Flask Port
EXPOSE 5000

# Zmq Sub Server
EXPOSE 4444

CMD ["python","/tmp/proxy.py"]
0 Answers
Related